changed/added license heading. Moved some json files to yaml
[osm/openvim.git] / charm / openvim / layer-openvim-compute / reactive / openvim.py
1 ##
2 # Copyright 2016
3 # This file is part of openvim
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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
15 # under the License.
16 #
17 ##
18
19 from os import chmod
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
26
27 def create_openvim_user():
28 status_set("maintenance", "Creating OpenVIM user")
29 ensure_user('openvim')
30
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')
35
36 def nopasswd_openvim_sudo():
37 status_set("maintenance", "Allowing nopasswd sudo for OpenVIM user")
38 with open('/etc/sudoers', 'r+') as f:
39 data = f.read()
40 if 'openvim ALL=(ALL) NOPASSWD:ALL' not in data:
41 f.seek(0)
42 f.truncate()
43 data += '\nopenvim ALL=(ALL) NOPASSWD:ALL\n'
44 f.write(data)
45
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')
50
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)
58
59 def download_default_image():
60 status_set("maintenance", "Downloading default image")
61 fetcher = ArchiveUrlFetchHandler()
62 fetcher.download(
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"
65 # TODO: add checksum
66 )
67
68 @when_not('openvim-compute.installed')
69 def prepare_openvim_compute():
70 create_openvim_user()
71 group_openvim_user()
72 nopasswd_openvim_sudo()
73 setup_qemu_binary()
74 setup_images_folder()
75 download_default_image()
76 status_set("active", "Ready")
77 set_state('openvim-compute.installed')
78
79 @when('compute.available', 'openvim-compute.installed')
80 def install_ssh_key(compute):
81 cache = kv()
82 if cache.get("ssh_key:" + compute.ssh_key()):
83 return
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)
89
90 @when('compute.connected')
91 def send_user(compute):
92 compute.send_user('openvim')