3 # This file is part of openvim
5 # Licensed under the Apache License, Version 2.0 (the "License"); you may
6 # not use this file except in compliance with the License. You may obtain
7 # a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 # License for the specific language governing permissions and limitations
20 from charms
.reactive
import when
, when_not
, set_state
21 from charmhelpers
.core
.hookenv
import status_set
22 from charmhelpers
.core
.unitdata
import kv
23 from charmhelpers
.core
.host
import mkdir
, symlink
, chownr
, add_user_to_group
24 from charmhelpers
.fetch
.archiveurl
import ArchiveUrlFetchHandler
25 from charmhelpers
.contrib
.unison
import ensure_user
27 def create_openvim_user():
28 status_set("maintenance", "Creating OpenVIM user")
29 ensure_user('openvim')
31 def group_openvim_user():
32 status_set("maintenance", "Adding OpenVIM user to groups")
33 add_user_to_group('openvim', 'libvirtd')
34 add_user_to_group('openvim', 'sudo')
36 def nopasswd_openvim_sudo():
37 status_set("maintenance", "Allowing nopasswd sudo for OpenVIM user")
38 with
open('/etc/sudoers', 'r+') as f
:
40 if 'openvim ALL=(ALL) NOPASSWD:ALL' not in data
:
43 data
+= '\nopenvim ALL=(ALL) NOPASSWD:ALL\n'
46 def setup_qemu_binary():
47 status_set("maintenance", "Setting up qemu-kvm binary")
48 mkdir('/usr/libexec', owner
='root', group
='root', perms
=0o775, force
=False)
49 symlink('/usr/bin/kvm', '/usr/libexec/qemu-kvm')
51 def setup_images_folder():
52 status_set("maintenance", "Setting up VM images folder")
53 mkdir('/opt/VNF', owner
='openvim', group
='openvim', perms
=0o775, force
=False)
54 symlink('/var/lib/libvirt/images', '/opt/VNF/images')
55 chownr('/opt/VNF', owner
='openvim', group
='openvim', follow_links
=False, chowntopdir
=True)
56 chownr('/var/lib/libvirt/images', owner
='root', group
='openvim', follow_links
=False, chowntopdir
=True)
57 chmod('/var/lib/libvirt/images', 0o775)
59 def download_default_image():
60 status_set("maintenance", "Downloading default image")
61 fetcher
= ArchiveUrlFetchHandler()
63 source
="https://cloud-images.ubuntu.com/releases/16.04/release/ubuntu-16.04-server-cloudimg-amd64-disk1.img",
64 dest
="/opt/VNF/images/ubuntu-16.04-server-cloudimg-amd64-disk1.img"
68 @when_not('openvim-compute.installed')
69 def prepare_openvim_compute():
72 nopasswd_openvim_sudo()
75 download_default_image()
76 status_set("active", "Ready")
77 set_state('openvim-compute.installed')
79 @when('compute.available', 'openvim-compute.installed')
80 def install_ssh_key(compute
):
82 if cache
.get("ssh_key:" + compute
.ssh_key()):
84 mkdir('/home/openvim/.ssh', owner
='openvim', group
='openvim', perms
=0o775)
85 with
open("/home/openvim/.ssh/authorized_keys", 'a') as f
:
86 f
.write(compute
.ssh_key() + '\n')
87 compute
.ssh_key_installed()
88 cache
.set("ssh_key:" + compute
.ssh_key(), True)
90 @when('compute.connected')
91 def send_user(compute
):
92 compute
.send_user('openvim')