Skip to content

Commit

Permalink
Adds Juju service removal script to /sbin via cloud-init.
Browse files Browse the repository at this point in the history
  • Loading branch information
manadart committed Nov 8, 2019
1 parent 489b953 commit 2fc1287
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
4 changes: 2 additions & 2 deletions cloudconfig/cloudinit/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ type RootUserConfig interface {
DisableRoot() bool
}

// WrittenFilesConfig is the interface for all file writing operaions.
// WrittenFilesConfig is the interface for all file writing operations.
type WrittenFilesConfig interface {
// AddRunTextFile simply issues some AddRunCmd's to set the contents of a
// given file with the specified file permissions on *first* boot.
Expand All @@ -329,7 +329,7 @@ type RenderConfig interface {
// It is used over ssh for bootstrapping with the manual provider.
RenderScript() (string, error)

// ShellRenderer renturns the shell renderer of this particular instance.
// ShellRenderer returns the shell renderer of this particular instance.
ShellRenderer() shell.Renderer

// getCommandsForAddingPackages is a helper function which returns all the
Expand Down
4 changes: 4 additions & 0 deletions cloudconfig/userdatacfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ install -D -m 600 /dev/null '/var/lib/juju/bootstrap-params'
printf '%s\\n' '.*' > '/var/lib/juju/bootstrap-params'
echo 'Installing Juju machine agent'.*
/var/lib/juju/tools/1\.2\.3-precise-amd64/jujud bootstrap-state --timeout 10m0s --data-dir '/var/lib/juju' --debug '/var/lib/juju/bootstrap-params'
install -D -m 755 /dev/null '/sbin/remove-juju-services'
printf '%s\\n' '.*' > '/sbin/remove-juju-services'
ln -s 1\.2\.3-precise-amd64 '/var/lib/juju/tools/machine-0'
echo 'Starting Juju machine agent \(service jujud-machine-0\)'.*
cat > /etc/init/jujud-machine-0\.conf << 'EOF'\\ndescription "juju agent for machine-0"\\nauthor "Juju Team <juju@lists\.ubuntu\.com>"\\nstart on runlevel \[2345\]\\nstop on runlevel \[!2345\]\\nrespawn\\nnormal exit 0\\n\\nlimit .*\\n\\nscript\\n\\n\\n # Ensure log files are properly protected\\n touch /var/log/juju/machine-0\.log\\n chown syslog:adm /var/log/juju/machine-0\.log\\n chmod 0640 /var/log/juju/machine-0\.log\\n\\n exec '/var/lib/juju/tools/machine-0/jujud' machine --data-dir '/var/lib/juju' --machine-id 0 --debug >> /var/log/juju/machine-0\.log 2>&1\\nend script\\nEOF\\n
Expand Down Expand Up @@ -404,6 +406,8 @@ printf %s '{"version":"1\.2\.3-quantal-amd64","url":"https://state-addr\.testing
mkdir -p '/var/lib/juju/agents/machine-99'
cat > '/var/lib/juju/agents/machine-99/agent\.conf' << 'EOF'\\n.*\\nEOF
chmod 0600 '/var/lib/juju/agents/machine-99/agent\.conf'
install -D -m 755 /dev/null '/sbin/remove-juju-services'
printf '%s\\n' '.*' > '/sbin/remove-juju-services'
ln -s 1\.2\.3-quantal-amd64 '/var/lib/juju/tools/machine-99'
echo 'Starting Juju machine agent \(service jujud-machine-99\)'.*
cat > /etc/init/jujud-machine-99\.conf << 'EOF'\\ndescription "juju agent for machine-99"\\nauthor "Juju Team <juju@lists\.ubuntu\.com>"\\nstart on runlevel \[2345\]\\nstop on runlevel \[!2345\]\\nrespawn\\nnormal exit 0\\n\\nlimit .*\\n\\nscript\\n\\n\\n # Ensure log files are properly protected\\n touch /var/log/juju/machine-99\.log\\n chown syslog:adm /var/log/juju/machine-99\.log\\n chmod 0640 /var/log/juju/machine-99\.log\\n\\n exec '/var/lib/juju/tools/machine-99/jujud' machine --data-dir '/var/lib/juju' --machine-id 99 --debug >> /var/log/juju/machine-99\.log 2>&1\\nend script\\nEOF\\n
Expand Down
29 changes: 28 additions & 1 deletion cloudconfig/userdatacfg_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,32 @@ while true; do
sleep {{.ToolsDownloadWaitTime}}
n=$((n+1))
done`

// removeServicesScript is written to /sbin and can be used to remove
// all Juju services from a machine.
// Once this script is run, logic to check whether such a machine is already
// provisioned should return false and the machine can be reused as a target
// for either bootstrap or add-machine.
removeServicesScript = `#!/bin/bash
# WARNING
# This script will clean a host previously used to run a Juju controller/machine.
# Running this on a live installation will render Juju inoperable.
for path_to_unit in $(ls /etc/systemd/system/juju*); do
echo "removing juju service: $path_to_unit"
unit=$(basename "$path_to_unit")
systemctl stop "$unit"
systemctl disable "$unit"
systemctl daemon-reload
done
echo "removing /var/lib/juju/db/*"
rm -rf /var/lib/juju/db/*
echo "removing /var/lib/juju/raft/*"
rm -rf /var/lib/juju/raft/*
`
)

var (
Expand Down Expand Up @@ -372,6 +398,8 @@ func (w *unixConfigure) ConfigureJuju() error {
}
}

w.conf.AddRunTextFile("/sbin/remove-juju-services", removeServicesScript, 0755)

return w.addMachineAgentToBoot()
}

Expand Down Expand Up @@ -610,7 +638,6 @@ func (w *unixConfigure) setUpGUI() (func(), error) {
// so it has a chance to add it to its catalogue.
w.conf.AddRunCmd("rm -f $gui/gui.tar.bz2 $gui/jujugui.sha256 $gui/downloaded-gui.txt")
}, nil

}

// toolsDownloadCommand takes a curl command minus the source URL,
Expand Down

0 comments on commit 2fc1287

Please sign in to comment.