Merge lp:~cprov/charms/trusty/adt-cloud-worker/nova-keypair-reset into lp:~canonical-ci-engineering/charms/trusty/adt-cloud-worker/trunk

Proposed by Celso Providelo
Status: Merged
Approved by: Celso Providelo
Approved revision: 7
Merged at revision: 7
Proposed branch: lp:~cprov/charms/trusty/adt-cloud-worker/nova-keypair-reset
Merge into: lp:~canonical-ci-engineering/charms/trusty/adt-cloud-worker/trunk
Diff against target: 90 lines (+41/-4)
2 files modified
hooks/actions.py (+40/-4)
hooks/services.py (+1/-0)
To merge this branch: bzr merge lp:~cprov/charms/trusty/adt-cloud-worker/nova-keypair-reset
Reviewer Review Type Date Requested Status
Para Siva (community) Approve
Review via email: mp+252095@code.launchpad.net

Commit message

Adding steps to (re-) configure nova keypairs, so worker can access its testbeds.

Description of the change

Adding steps to (re-) configure nova keypairs, so worker can access its testbeds.

On install or every configuration change the charm checks for a existing SSH key (for root), if it exists we assume it was already setup and nothing is done. If it doesn't exist, we create a new default SSH key (RSA-2048, empty-pass, ~/.ssh/id_rsa) and enable (del + add) it in nova using the current app configuration nova credentials and named as the configuration worker name.

It requires us to change the way adt-run is called by additionally passing '-k <worker_name>', I've tested it by abusing nova.extra_args configuration parameter, but ideally we would change a-c-w code for doing that automatically.

The side-effect of this change is that the configuration nova credentials will have as many keys as deployed workers, which might require some future cleanup. Re-deploying units is already nicely supported because before adding a new keypair we delete it, so it's effectively updated.

To post a comment you must log in.
Revision history for this message
Para Siva (psivaa) wrote :

+1, looks good.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'hooks/actions.py'
2--- hooks/actions.py 2015-03-05 03:37:14 +0000
3+++ hooks/actions.py 2015-03-06 12:07:13 +0000
4@@ -1,4 +1,5 @@
5 import base64
6+import ConfigParser as configparser
7 import os
8 import shutil
9 import subprocess
10@@ -8,29 +9,64 @@
11
12
13 REQUIRED_PACKAGES = [
14- 'bzr', 'autopkgtest', 'python-virtualenv', 'python3-dev']
15+ 'bzr', 'autopkgtest', 'python-novaclient', 'python-virtualenv',
16+ 'python3-dev']
17 SERVICE_DIR = '/srv/adt-cloud-worker'
18
19 config = hookenv.config()
20
21+
22 def log_start(service_name):
23 hookenv.log('adt-cloud-worker starting')
24
25+
26 def install_packages(service_name):
27 hookenv.log('Installing dependencies...')
28 fetch.add_source('ppa:canonical-ci-engineering/ci-airline-phase-0')
29 fetch.configure_sources(update=True)
30 fetch.apt_install(REQUIRED_PACKAGES, fatal=True)
31
32+
33 def get_cloud_worker_branch(service_name):
34 branch = config['branch']
35 shutil.rmtree(SERVICE_DIR, ignore_errors=True)
36 subprocess.call(['bzr', 'branch', branch, SERVICE_DIR])
37
38+
39 def get_config_file(service_name):
40- config_file = config['config-file']
41- with open(os.path.join(SERVICE_DIR, '.adt-service.conf'), 'w') as f:
42- f.write(base64.b64decode(config_file))
43+ config_content = base64.b64decode(config['config-file'])
44+ config_path = os.path.join(SERVICE_DIR, '.adt-service.conf')
45+ with open(config_path, 'w') as f:
46+ f.write(config_content)
47+
48+
49+def reset_nova(service_name):
50+ hookenv.log('Attempting to reset nova key-pairs ...')
51+ pubkey_path = os.path.expanduser('~/.ssh/id_rsa.pub')
52+ if os.path.exists(pubkey_path):
53+ hookenv.log('SSH key already exists, nothing to do ...')
54+ return
55+
56+ hookenv.log('Generating a new SSH key ...')
57+ seckey_path = pubkey_path[:-4]
58+ subprocess.call('ssh-keygen -f %s -q -N ""' % seckey_path, shell=True)
59+
60+ config_path = os.path.join(SERVICE_DIR, '.adt-service.conf')
61+ hookenv.log('Using current app configuration from: %s' % config_path)
62+ config = configparser.ConfigParser()
63+ config.read(config_path)
64+ for k, v in config.items('nova'):
65+ if not k.startswith('os_'):
66+ continue
67+ os.environ[k.upper()] = str(v)
68+
69+ worker_name = config.get('adt', 'name')
70+ hookenv.log('Resetting keypair "%s" ...' % worker_name)
71+ subprocess.call('nova keypair-delete %s' % worker_name, shell=True)
72+ subprocess.call(
73+ 'nova keypair-add --pub-key %s %s' % (pubkey_path, worker_name),
74+ shell=True)
75+
76
77 def install_python_packages(service_name):
78 hookenv.log('Installing python packages...')
79
80=== modified file 'hooks/services.py'
81--- hooks/services.py 2015-03-05 01:09:53 +0000
82+++ hooks/services.py 2015-03-06 12:07:13 +0000
83@@ -18,6 +18,7 @@
84 actions.get_cloud_worker_branch,
85 actions.install_python_packages,
86 actions.get_config_file,
87+ actions.reset_nova,
88 helpers.render_template(
89 source='upstart.conf',
90 target='/etc/init/adt-cloud-worker.conf'),

Subscribers

People subscribed via source and target branches