2 from charms
.reactive
import when
, when_not
, set_state
3 from charmhelpers
.core
.hookenv
import status_set
4 from charmhelpers
.core
.unitdata
import kv
5 from charmhelpers
.core
.host
import mkdir
, symlink
, chownr
, add_user_to_group
6 from charmhelpers
.fetch
.archiveurl
import ArchiveUrlFetchHandler
7 from charmhelpers
.contrib
.unison
import ensure_user
9 def create_openvim_user():
10 status_set("maintenance", "Creating OpenVIM user")
11 ensure_user('openvim')
13 def group_openvim_user():
14 status_set("maintenance", "Adding OpenVIM user to groups")
15 add_user_to_group('openvim', 'libvirtd')
16 add_user_to_group('openvim', 'sudo')
18 def nopasswd_openvim_sudo():
19 status_set("maintenance", "Allowing nopasswd sudo for OpenVIM user")
20 with
open('/etc/sudoers', 'r+') as f
:
22 if 'openvim ALL=(ALL) NOPASSWD:ALL' not in data
:
25 data
+= '\nopenvim ALL=(ALL) NOPASSWD:ALL\n'
28 def setup_qemu_binary():
29 status_set("maintenance", "Setting up qemu-kvm binary")
30 mkdir('/usr/libexec', owner
='root', group
='root', perms
=0o775, force
=False)
31 symlink('/usr/bin/kvm', '/usr/libexec/qemu-kvm')
33 def setup_images_folder():
34 status_set("maintenance", "Setting up VM images folder")
35 mkdir('/opt/VNF', owner
='openvim', group
='openvim', perms
=0o775, force
=False)
36 symlink('/var/lib/libvirt/images', '/opt/VNF/images')
37 chownr('/opt/VNF', owner
='openvim', group
='openvim', follow_links
=False, chowntopdir
=True)
38 chownr('/var/lib/libvirt/images', owner
='root', group
='openvim', follow_links
=False, chowntopdir
=True)
39 chmod('/var/lib/libvirt/images', 0o775)
41 def download_default_image():
42 status_set("maintenance", "Downloading default image")
43 fetcher
= ArchiveUrlFetchHandler()
45 source
="https://cloud-images.ubuntu.com/releases/16.04/release/ubuntu-16.04-server-cloudimg-amd64-disk1.img",
46 dest
="/opt/VNF/images/ubuntu-16.04-server-cloudimg-amd64-disk1.img"
50 @when_not('openvim-compute.installed')
51 def prepare_openvim_compute():
54 nopasswd_openvim_sudo()
57 download_default_image()
58 status_set("active", "Ready")
59 set_state('openvim-compute.installed')
61 @when('compute.available', 'openvim-compute.installed')
62 def install_ssh_key(compute
):
64 if cache
.get("ssh_key:" + compute
.ssh_key()):
66 mkdir('/home/openvim/.ssh', owner
='openvim', group
='openvim', perms
=0o775)
67 with
open("/home/openvim/.ssh/authorized_keys", 'a') as f
:
68 f
.write(compute
.ssh_key() + '\n')
69 compute
.ssh_key_installed()
70 cache
.set("ssh_key:" + compute
.ssh_key(), True)
72 @when('compute.connected')
73 def send_user(compute
):
74 compute
.send_user('openvim')