Esempio n. 1
0
 def __init__(self, container):
     self.data = container
     self.packages = {
         'Ubuntu 14.*': [
             'libvirt-bin',
             'qemu',
             'wget',
             'genisoimage',
         ],
         'CentOS Linux 7.*': [
             'epel-release',
             'libvirt',
             'virt-install',
             'qemu',
             'wget',
             'genisoimage',
         ],
     }
     self.services = [
         'libvirtd',
     ]
     # self.libvirt_dir = os.path.join(CONF._storage_dir, 'container', 'libvirt')
     self.libvirt_dir = os.path.join('/opt/fabkit', 'container', 'libvirt')
     self.template_dir = os.path.join(os.path.dirname(__file__), 'templates')
     self.instances_dir = os.path.join(self.libvirt_dir, 'instances')
     filer.mkdir(self.instances_dir)
Esempio n. 2
0
 def mount_local(self):
     data = self.init()
     for volume in data['volume_map'].values():
         filer.Editor('/etc/fstab').a('localhost:/{0} /mnt/{0} glusterfs '
                                      'defaults,_netdev 0 0'.format(volume['name']))
         filer.mkdir('/mnt/{0}'.format(volume['name']))
         sudo('mount -a')
Esempio n. 3
0
def setup():
    filer.mkdir('/tmp/fabkit')
    filer.file(src='file_sample.txt', dest='/tmp/fabkit/file_sample.txt',
               override=True)
    filer.template(src='template_sample.txt',
                   dest='/tmp/fabkit/template_sample.txt', data={'msg': 'test'})
    run('diff /tmp/fabkit/file_sample.txt /tmp/fabkit/template_sample.txt')
Esempio n. 4
0
    def setup(self):
        data = self.init()

        if self.is_tag('package'):
            self.python.setup()
            self.python.setup_package(**self.package)

            filer.mkdir(data['glance_store']['filesystem_store_datadir'])

        if self.is_tag('conf'):
            # setup conf files
            if filer.template(
                    '/etc/glance/glance-api.conf',
                    src='{0}/glance-api.conf.j2'.format(data['version']),
                    data=data):
                self.handlers['restart_glance-api'] = True

            if filer.template(
                    '/etc/glance/glance-registry.conf',
                    src='{0}/glance-registry.conf.j2'.format(data['version']),
                    data=data):
                self.handlers['restart_glance-registry'] = True

        if self.is_tag('data') and env.host == env.hosts[0]:
            sudo('{0}/bin/glance-manage db_sync'.format(self.prefix))

        if self.is_tag('conf', 'service'):
            self.enable_services().start_services(pty=False)
            self.exec_handlers()
Esempio n. 5
0
 def __init__(self, container):
     self.data = container
     self.packages = {
         'Ubuntu 14.*': [
             'libvirt-bin',
             'qemu',
             'wget',
             'genisoimage',
         ],
         'CentOS Linux 7.*': [
             'epel-release',
             'libvirt',
             'virt-install',
             'qemu',
             'wget',
             'genisoimage',
         ],
     }
     self.services = [
         'libvirtd',
     ]
     # self.libvirt_dir = os.path.join(CONF._storage_dir, 'container', 'libvirt')
     self.libvirt_dir = os.path.join('/opt/fabkit', 'container', 'libvirt')
     self.template_dir = os.path.join(os.path.dirname(__file__), 'templates')
     self.instances_dir = os.path.join(self.libvirt_dir, 'instances')
     filer.mkdir(self.instances_dir)
     self.pdns = pdnsapi.PdnsAPI()
Esempio n. 6
0
 def mount_local(self):
     data = self.init()
     for volume in data["volume_map"].values():
         filer.Editor("/etc/fstab").a(
             "localhost:/{0} /mnt/{0} glusterfs " "defaults,_netdev 0 0".format(volume["name"])
         )
         filer.mkdir("/mnt/{0}".format(volume["name"]))
         sudo("mount -a")
Esempio n. 7
0
def setup():
    filer.mkdir('/tmp/fabkit')
    filer.file(src='file_sample.txt',
               dest='/tmp/fabkit/file_sample.txt',
               override=True)
    filer.template(src='template_sample.txt',
                   dest='/tmp/fabkit/template_sample.txt',
                   data={'msg': 'test'})
    run('diff /tmp/fabkit/file_sample.txt /tmp/fabkit/template_sample.txt')
Esempio n. 8
0
    def start(self, targets=None):
        if targets is not None:
            self.targets = targets

        filer.mkdir(CONF._remote_stats_dir)
        dstat_csv = CONF._remote_stats_dir + '/dstat.csv'
        dstat_out = CONF._remote_stats_dir + '/dstat.out'
        sudo("rm -rf {0}".format(dstat_csv))
        sudo("sh -c 'dstat -tTlpyirdfmsgn --tcp --socket --vm --lock --aio --fs --ipc --output {0} > {1} &'".format(dstat_csv, dstat_out), pty=False)
Esempio n. 9
0
    def setup(self):
        data = self.init()

        Service('firewalld').stop().disable()

        self.install_packages()
        self.start_services().enable_services()

        for volume in data['volume_map'].values():
            filer.mkdir(volume['brick'])
Esempio n. 10
0
    def setup(self):
        data = self.init()

        Service("firewalld").stop().disable()

        self.install_packages()
        self.start_services().enable_services()

        for volume in data["volume_map"].values():
            filer.mkdir(volume["brick"])
Esempio n. 11
0
    def create(self):
        data = self.data
        sudo('modprobe kvm')
        sudo('modprobe kvm_intel')

        for i, vm in enumerate(data['libvirt_vms']):
            instance_dir = os.path.join(self.instances_dir, vm['name'])
            filer.mkdir(instance_dir)

            image_path = '{0}/vm.img'.format(instance_dir)
            vm['image_path'] = image_path
            src_image_path = self.wget_src_image(vm)
            if not filer.exists(image_path):
                sudo('cp {0} {1}'.format(src_image_path, image_path))
                sudo('qemu-img resize {0} {1}G'.format(image_path, vm.get('disk_size', 10)))

            configiso_path = self.create_configiso(vm, instance_dir)
            vm['configiso_path'] = configiso_path

            vm['mac'] = self.get_random_mac()

            domain_xml = self.create_domain_xml(vm, instance_dir)

            sudo("sed -i 's/^Defaults.*requiretty/# Defaults requiretty/' /etc/sudoers")

            sudo("virsh net-update default add ip-dhcp-host "
                 "\"<host mac='{0}' name='{1}' ip='{2}' />\"".format(
                     vm['mac'], vm['name'], vm['ip']))

            sudo('virsh define {0}'.format(domain_xml))
            sudo('chown -R root:root {0}'.format(instance_dir))
            sudo('virsh start {0}'.format(vm['name']))

        for vm in data['libvirt_vms']:
            while True:
                with api.warn_only():
                    if run('nmap -p 22 {0} | grep open'.format(vm['ip'])):
                        break
                    time.sleep(5)

        sudo("iptables -R FORWARD 1 -o virbr0 -s 0.0.0.0/0"
             " -d 192.168.122.0/255.255.255.0 -j ACCEPT")
        for vm in data['libvirt_vms']:
            for port in vm.get('ports', []):
                sudo("iptables -t nat -A PREROUTING -p tcp"
                     " --dport {0[1]} -j DNAT --to {1}:{0[0]}".format(
                         port, vm['ip']))

        for ip in data['iptables']:
            for port in ip.get('ports', []):
                sudo("iptables -t nat -A PREROUTING -p tcp"
                     " --dport {0[1]} -j DNAT --to {1}:{0[0]}".format(
                         port, ip['ip']))

        time.sleep(5)
Esempio n. 12
0
    def setup(self):
        data = self.init()
        if RE_CENTOS.match(env.node['os']):
            sudo('setenforce 0')

        self.install_packages()
        filer.mkdir('/var/log/corosync')
        Service('haproxy').disable()

        sudo("sh -c \"echo 'hacluster:{0}' |chpasswd\"".format(
            data['ha_password']))
        Service('pcsd').start().enable()
Esempio n. 13
0
    def setup(self):
        data = self.init()

        if self.is_tag('package'):
            self.install_packages()
            self.python.setup()
            self.python.setup_package(**self.package)

        if self.is_tag('conf'):
            # setup conf files
            if filer.template(
                    '/etc/swift/swift.conf',
                    src='{0}/swift/swift.conf.j2'.format(data['version']),
                    data=data):
                self.handlers['restart_swift-proxy-server'] = True

            if filer.template(
                    '/etc/swift/proxy-server.conf',
                    src='{0}/swift/proxy-server.conf.j2'.format(data['version']),
                    data=data):
                self.handlers['restart_swift-proxy-server'] = True

            if filer.template(
                    '/etc/swift/account-server.conf',
                    src='{0}/swift/account-server.conf.j2'.format(data['version']),
                    data=data):
                self.handlers['restart_swift-account-server'] = True

            if filer.template(
                    '/etc/swift/container-server.conf',
                    src='{0}/swift/container-server.conf.j2'.format(data['version']),
                    data=data):
                self.handlers['restart_swift-container-server'] = True

            if filer.template(
                    '/etc/swift/object-server.conf',
                    src='{0}/swift/object-server.conf.j2'.format(data['version']),
                    data=data):
                self.handlers['restart_swift-object-server'] = True

        if self.is_tag('data'):
            filer.mkdir('/mnt/swift/main')
            for ring in ['account', 'object', 'container']:
                port = data['{0}_port'.format(ring)]
                sudo('cd /etc/swift &&'
                     '[ -e {0}.builder ] || swift-ring-builder {0}.builder create 9 1 1 && '
                     '[ -e {0}.ring.gz ] || swift-ring-builder {0}.builder add z1-127.0.0.1:{1}/main 100 &&'  # noqa
                     '[ -e {0}.ring.gz ] || swift-ring-builder {0}.builder rebalance'.format(ring, port))  # noqa

        if self.is_tag('conf', 'service'):
            self.enable_services().start_services(pty=False)
            self.exec_handlers()
Esempio n. 14
0
    def setup_package(self, name, git_repos=[], exec_user='******', branch=None, is_develop=False,
                      mk_links=[], mk_dirs=[], cp_files=[], services=[], requirements=[], **kwargs):

        user.add(exec_user)

        for git_repo in git_repos:
            git_dir = os.path.join(self.prefix, 'src', git_repo['name'])
            git_dir_parent = os.path.dirname(git_dir)
            filer.mkdir(git_dir_parent, mode='777')

            git.sync(git_repo['url'], branch=git_repo['branch'], dest=git_dir)

            requirements_txt = '{0}/requirements.txt'.format(git_dir)
            if filer.exists(requirements_txt):
                self.install(requirements=requirements_txt)

            if is_develop:
                sudo('sh -c "cd {0} && {1}/bin/python setup.py develop"'.format(
                    git_dir, self.prefix))
            else:
                sudo('sh -c "cd {0} && {1}/bin/python setup.py install"'.format(
                    git_dir, self.prefix))

        for mkdir in mk_dirs:
            filer.mkdir(mkdir['path'], owner=mkdir.get('owner', exec_user))

        for cpfile in cp_files:
            if filer.exists(cpfile['dest']):
                continue

            sudo('cp -r {0} {1}'.format(
                os.path.join(git_dir, cpfile['src']), cpfile['dest']))

        for service in services:
            service['user'] = exec_user
            filer.template(
                '/etc/systemd/system/{0}.service'.format(service['name']),
                src='systemd.service.j2', mode='755',
                data=service)

        for requirement in requirements:
            self.install(requirement)

        for link in mk_links:
            if not filer.exists(link['dest']):
                sudo('ln -s {0} {1}'.format(link['src'], link['dest']))
Esempio n. 15
0
    def wget_src_image(self, vm):
        images_dir = os.path.join(self.libvirt_dir, 'images')
        filer.mkdir(images_dir)

        src_image = vm['src_image'].rsplit('/', 1)[1]
        src_image_path = '{0}/{1}'.format(images_dir, src_image)
        src_image_format = 'qcow2'

        if src_image_path[-3:] == '.xz':
            src_image_path = src_image_path[:-3]
            src_image_format = 'xz'

        if not filer.exists(src_image_path):
            sudo('cd {0} && wget {1}'.format(images_dir, vm['src_image']))

            if src_image_format == 'xz':
                sudo('cd {0} && xz -d {1}'.format(images_dir, src_image))

        return src_image_path
Esempio n. 16
0
    def wget_src_image(self, vm):
        images_dir = os.path.join(self.libvirt_dir, 'images')
        filer.mkdir(images_dir)

        src_image = vm['src_image'].rsplit('/', 1)[1]
        src_image_path = '{0}/{1}'.format(images_dir, src_image)
        src_image_format = 'qcow2'

        if src_image_path[-3:] == '.xz':
            src_image_path = src_image_path[:-3]
            src_image_format = 'xz'

        if not filer.exists(src_image_path):
            sudo('sh -c "cd {0} && wget {1}"'.format(images_dir, vm['src_image']))

            if src_image_format == 'xz':
                sudo('sh -c "cd {0} && xz -d {1}"'.format(images_dir, src_image))

        return src_image_path
Esempio n. 17
0
    def install_graphite_web(self):
        data = self.data

        Package('pycairo').install()
        Package('cairo-devel').install()
        Package('bitmap-fonts-compat').install()
        Package('httpd').install()
        Package('mod_wsgi').install()
        Package('MySQL-python').install()

        python.setup()
        sudo('pip install django==1.6.8')
        python.install_from_git('graphite-web',
                                'https://github.com/graphite-project/graphite-web.git')

        log_dir = '/opt/graphite/storage/log/webapp/'
        owner = '{0[user]}:{0[group]}'.format(data)
        filer.mkdir(log_dir, owner=owner)
        log_files = ['access.log', 'error.log', 'exception.log', 'info.log']
        for log_file in log_files:
            log_file = os.path.join(log_dir, log_file)
            filer.touch(log_file, owner=owner)

        manage_py = os.path.join(conf.REMOTE_TMP_DIR, 'git/graphite-web.git/webapp/manage.py')
        sudo('cp {0} /opt/graphite/webapp/'.format(manage_py))

        is_updated = filer.template('/opt/graphite/webapp/graphite/local_settings.py',
                                    data=data)

        is_updated = filer.template('/opt/graphite/webapp/graphite/settings.py',
                                    data=data) or is_updated

        is_updated = filer.template('/opt/graphite/conf/graphite.wsgi') or is_updated

        is_updated = filer.template('/etc/httpd/conf.d/graphite-vhost.conf', data={
            'user': data['user'],
            'group': data['group'],
        }) or is_updated

        return is_updated
Esempio n. 18
0
        def sub_wrapper(*args, **kwargs):
            env.args = args if args else []
            env.kwargs = kwargs if kwargs else {}
            env.node = env.node_map[env.host]
            env.node.update(env.node_status_map[env.host]['fabscript_map'][
                env.script_name])
            kwargs = env.host_script_map.get(env.host,
                                             {}).get(env.script_name, {})
            if is_bootstrap:
                bootstrap_status = env.node_map[env.host]['bootstrap_status']
                if not bootstrap_status == status.SUCCESS:
                    with api.hide(*CONF.system_output_filter):
                        result = check_basic()
                        if result['task_status'] != status.SUCCESS:
                            result['node'] = env.node
                            return result

                    filer.mkdir(CONF._remote_dir,
                                owner='{0}:root'.format(env.user),
                                mode='770')
                    filer.mkdir(CONF._remote_storage_dir,
                                owner='{0}:root'.format(env.user),
                                mode='770')
                    filer.mkdir(CONF._remote_tmp_dir,
                                owner='{0}:root'.format(env.user),
                                mode='770')

            try:
                result = func(**kwargs)
            except:
                exc_type, exc_value, exc_traceback = sys.exc_info()
                msg = traceback.format_exception(exc_type, exc_value,
                                                 exc_traceback)
                log.error(''.join(msg))
                result = {'task_status': status.FAILED}

            if result is None:
                result = {}

            result['node'] = env.node

            return result
Esempio n. 19
0
        def sub_wrapper(*args, **kwargs):
            env.args = args if args else []
            env.kwargs = kwargs if kwargs else {}
            env.node = env.node_map[env.host]
            env.node.update(env.node_status_map[env.host]['fabscript_map'][env.script_name])
            kwargs = env.host_script_map.get(env.host, {}).get(env.script_name, {})
            if is_bootstrap:
                bootstrap_status = env.node_map[env.host]['bootstrap_status']
                if not bootstrap_status == status.SUCCESS:
                    with api.hide(*CONF.system_output_filter):
                        result = check_basic()
                        if result['task_status'] != status.SUCCESS:
                            result['node'] = env.node
                            return result

                    filer.mkdir(CONF._remote_dir, owner='{0}:root'.format(env.user), mode='770')
                    filer.mkdir(CONF._remote_storage_dir, owner='{0}:root'.format(env.user),
                                mode='770')
                    filer.mkdir(CONF._remote_tmp_dir, owner='{0}:root'.format(env.user),
                                mode='770')

            try:
                result = func(**kwargs)
            except:
                exc_type, exc_value, exc_traceback = sys.exc_info()
                msg = traceback.format_exception(exc_type, exc_value, exc_traceback)
                log.error(''.join(msg))
                result = {
                    'task_status': status.FAILED
                }

            if result is None:
                result = {}

            result['node'] = env.node

            return result
Esempio n. 20
0
    def setup(self):
        self.install_packages()
        self.start_services().enable_services()

        repo = '/home/{0}/fabkit-repo'.format(env.user)
        filer.mkdir(repo, use_sudo=False)
        git.setup()
        git.sync('https://github.com/fabrickit/fabkit.git',
                 dest='{0}/fabfile'.format(repo))

        python = Python('/opt/fabkit')
        python.setup()
        python.install(
            requirements='{0}/fabfile/requirements.txt'.format(repo))

        run('cd {0} && /opt/fabkit/bin/fab genconfig:fabfile.ini &&'
            ' sed -i "/^\[web\]/,/^\[/s/#hostname =.*/hostname = */g" fabfile.ini'.format(repo))

        data = {
            'port': 80,
            'repo': repo,
            'user': env.user,
            'group': env.user,
            'python_path': python.get_site_packages(),
            'processes': 5,
            'threads': 1,
        }

        run('cd {0}/fabfile/core/webapp/ &&'
            ' /opt/fabkit/bin/python manage.py migrate &&'
            ' echo "from django.contrib.auth.models import User;'
            '       User.objects.create_superuser(\'admin\', \'admin@localhost\', \'admin\')"'
            '       | /opt/fabkit/bin/python manage.py shell &&'
            ' /opt/fabkit/bin/python manage.py collectstatic --noinput'.format(repo))

        sudo('chmod 755 /home/{0}'.format(env.user))

        if re.match('CentOS .*', env.node['os']):
            log_prefix = '/var/log/httpd/{0}'.format(env.user)
            data['error_log'] = '{0}-error.log'.format(log_prefix)
            data['custom_log'] = '{0}-access.log'.format(log_prefix)

            if filer.template(src='httpd.conf',
                              dest='/etc/httpd/conf.d/{0}_httpd.conf'.format(env.user),
                              data=data):
                self.handlers['restart_httpd'] = True

        elif re.match('Ubuntu .*', env.node['os']):
            log_prefix = '/var/log/apache2/{0}'.format(env.user)
            data['error_log'] = '{0}-error.log'.format(log_prefix)
            data['custom_log'] = '{0}-access.log'.format(log_prefix)

            if filer.template(src='httpd.conf',
                              dest='/etc/apache2/sites-enabled/{0}_httpd.conf'.format(env.user),
                              data=data):
                self.handlers['restart_apache2'] = True

        self.exec_handlers()

        # install
        sudo('npm install -g coffee-script'.format(repo))
        sudo('cd {0}/fabfile/core/webapp && npm install'.format(repo))

        # install node packages for develop
        sudo('npm install -g grunt-cli'.format(repo))
        sudo('cd {0}/fabfile/core/webapp/node_chat && npm install'.format(repo))
Esempio n. 21
0
    def create(self):
        data = self.data
        sudo('modprobe kvm')
        sudo('modprobe kvm_intel')

        network = CONF.network.libvirt_net.split(':')
        bridge = network[0]
        brctl_show = sudo('brctl show')
        if brctl_show.find(bridge) == -1:
            sudo('brctl addbr {0}'.format(bridge))

        ip_network = IPNetwork(network[1])
        gateway_ip = '{0}/{1}'.format(ip_network.ip + 1, ip_network.prefixlen)
        dhcp_ip = '{0}/{1}'.format(ip_network.ip + 2, ip_network.prefixlen)
        bridge_info = sudo('ip addr show dev {0}'.format(bridge))
        if bridge_info.find(gateway_ip) == -1:
            sudo('ip addr add {0} dev {1}'.format(gateway_ip, bridge))
        if bridge_info.find('DOWN') != -1:
            sudo('ip link set {0} up'.format(bridge))
            sudo('ip route add 10.0.0.0/8 via {0}'.format(ip_network.ip + 1))

        network_seg = "{0}/{1}".format(ip_network.ip, ip_network.netmask)

        ip_netns = sudo('ip netns show')
        dhcp_netns = 'dhcp-{0}'.format(bridge)
        dhcp_veth_br = 'ns-{0}'.format(bridge)
        dhcp_veth = 'veth-{0}'.format(bridge)
        if ip_netns.find(dhcp_netns):
            sudo('ip netns add {0}'.format(dhcp_netns))

        if brctl_show.find(dhcp_veth_br) == -1:
            sudo('ip link add {0} type veth peer name {1}'.format(dhcp_veth_br, dhcp_veth))
            sudo('brctl addif {0} {1}'.format(bridge, dhcp_veth_br))
            sudo('ip link set {0} up'.format(dhcp_veth_br))
            sudo('ip link set {0} netns {1}'.format(dhcp_veth, dhcp_netns))
            sudo('ip netns exec {0} ip addr add dev {1} {2}'.format(dhcp_netns, dhcp_veth, dhcp_ip))
            sudo('ip netns exec {0} ip link set {1} up'.format(dhcp_netns, dhcp_veth))

        # ss_ln = sudo('ip netns exec {0} ss -ln'.format(dhcp_netns))
        # if ss_ln.find('*:67') == -1:
        #     sudo('ip netns exec {0} dnsmasq -p 0 --dhcp-range 172.16.100.3,172.16.100.254,12h'.format(  # noqa
        #         dhcp_netns, ip_network[3], ip_network[-2]))
        ss_ln = sudo('ss -ln'.format(dhcp_netns))
        if ss_ln.find('*:67') == -1:
            sudo('dnsmasq -p 0 --dhcp-range=172.16.100.3,172.16.100.254')

        for i, vm in enumerate(data['libvirt_vms']):
            instance_dir = os.path.join(self.instances_dir, vm['name'])
            filer.mkdir(instance_dir)

            vm['bridge'] = bridge
            vm['hostname'] = '{0}.{1}'.format(vm['name'], CONF.network.domain)

            image_path = '{0}/vm.img'.format(instance_dir)
            vm['image_path'] = image_path
            src_image_path = self.wget_src_image(vm)
            if not filer.exists(image_path):
                sudo('cp {0} {1}'.format(src_image_path, image_path))
                sudo('qemu-img resize {0} {1}G'.format(image_path, vm.get('disk_size', 10)))
            if 'disk_cache' not in vm:
                vm['disk_cache'] = 'none'
            elif vm['disk_cache'] not in ['none', 'writethrough', 'writeback',
                                          'directsync', 'unsafe', 'default']:
                raise Exception('Invalid disk_cache: {0}'.format(vm['disk_cache']))

            configiso_path = self.create_configiso(vm, instance_dir)
            vm['configiso_path'] = configiso_path

            alias_index = 0
            pci_slot_num = 2
            for port in vm['ports']:
                mac = self.get_random_mac()
                port['mac'] = mac
                port['tap'] = 'tap{0}'.format(mac.replace(':', ''))

                port['pci_slot'] = '0x0{0}'.format(pci_slot_num)
                pci_slot_num += 1

                port['alias_name'] = 'net{0}'.format(alias_index)
                alias_index += 1

            vm['memballoon'] = {
                'pci_slot': '0x0{0}'.format(pci_slot_num)
            }

            domain_xml = self.create_domain_xml(vm, instance_dir)

            sudo("sed -i 's/^Defaults.*requiretty/# Defaults requiretty/' /etc/sudoers")

            for port in vm['ports']:
                if port['ip'] == 'none':
                    continue

                # sudo("virsh net-update {3} add ip-dhcp-host "
                #      "\"<host mac='{0}' name='{1}' ip='{2}' />\"".format(
                #          port['mac'], vm['name'], port['ip'], bridge))

            sudo('virsh define {0}'.format(domain_xml))
            sudo('chown -R root:root {0}'.format(instance_dir))
            sudo('virsh start {0}'.format(vm['name']))

        nat_table = sudo("iptables -t nat -L")
        if nat_table.find(network_seg) == -1:
            # sudo("iptables -R FORWARD 1 -o {0} -s {1}"
            #      " -d 0.0.0.0/0 -j ACCEPT".format(bridge, network_seg))
            sudo("iptables -t filter -A FORWARD -s 0.0.0.0/0 -d {0} -j ACCEPT".format(network_seg))
            sudo("iptables -t filter -A FORWARD -d 0.0.0.0/0 -s {0} -j ACCEPT".format(network_seg))

        nat_table = sudo("iptables -t nat -L")
        if nat_table.find(network_seg) == -1:
            sudo("iptables -t nat -A POSTROUTING -p TCP -s {0} ! -d {0} -j MASQUERADE --to-ports 1024-65535".format(
                network_seg))
            sudo("iptables -t nat -A POSTROUTING -p UDP -s {0} ! -d {0} -j MASQUERADE --to-ports 1024-65535".format(
                network_seg))
            sudo("iptables -t nat -A POSTROUTING -s {0} ! -d {0} -j MASQUERADE".format(
                network_seg))
            sudo("iptables -t nat -A POSTROUTING -s {0} -d 255.255.255.255 -j RETURN".format(
                network_seg))
            sudo("iptables -t nat -A POSTROUTING -s {0} -d base-address.mcast.net/24 -j RETURN".format(
                network_seg))

        for vm in data['libvirt_vms']:
            self.pdns.create_record(vm['name'], CONF.network.domain, 'A', vm['ports'][0]['ip'])

            while True:
                with api.warn_only():
                    if run('nmap -p 22 {0} | grep open'.format(vm['ports'][0]['ip'])):
                        break
                    time.sleep(5)

        for ip in data.get('iptables', {}):
            for port in ip.get('ports', []):
                if ip['ip'] == 'none':
                    continue
                sudo("iptables -t nat -A PREROUTING -p tcp"
                     " --dport {0[1]} -j DNAT --to {1}:{0[0]}".format(
                         port, ip['ip']))

        time.sleep(60)
Esempio n. 22
0
    def setup(self):
        data = self.init()

        var_dir = CONF.client.package_var_dir
        tmp_dir = os.path.join(var_dir, 'tmp')
        log_dir = '/var/log/fabkit'
        common_repo = '{0}/fabkit-repo-common'.format(var_dir)
        client_repo = '{0}/fabkit-repo-client'.format(var_dir)
        server_repo = '{0}/fabkit-repo-server'.format(var_dir)

        if data['use_package']:
            Package(data['package_name']).install()
            return

        self.install_packages()
        self.python.setup()
        filer.mkdir(var_dir, owner=data['owner'])
        filer.mkdir(log_dir, owner=data['owner'])
        filer.mkdir(tmp_dir, owner=data['owner'], mode='777')

        filer.mkdir(common_repo, owner=data['owner'])
        filer.mkdir(client_repo, owner=data['owner'])
        filer.mkdir(server_repo, owner=data['owner'])

        sudo('rm -rf {0}/fabfile*'.format(tmp_dir))
        fabfile_tar_gz = os.path.join(tmp_dir, 'fabfile.tar.gz')
        scp('/tmp/fabfile.tar.gz', fabfile_tar_gz)

        sudo('rm -rf {0}/fabfile'.format(common_repo))
        sudo('cd {0} && tar xzf {1} && '
             'cp -r fabfile {2}/fabfile'.format(tmp_dir, fabfile_tar_gz,
                                                common_repo))

        sudo('{0}/bin/pip install -r {1}/fabfile/requirements.txt'.format(
            CONF.client.package_prefix, common_repo))

        data['repo'] = client_repo
        filer.template('{0}/bin/fabclient'.format(CONF.client.package_prefix),
                       src='fabric.sh',
                       data=data,
                       mode='755')

        data['repo'] = server_repo
        filer.template('{0}/bin/fabserver'.format(CONF.client.package_prefix),
                       src='fabric.sh',
                       data=data,
                       mode='755')

        filer.template('{0}/bin/fabnode'.format(CONF.client.package_prefix),
                       src='fabnode.sh',
                       data=data,
                       mode='755')

        filer.template('/etc/systemd/system/fabagent.service',
                       src='systemd.service',
                       data={
                           'description': 'fabagent',
                           'exec': '/opt/fabkit/bin/fabclient agent',
                           'user': '******',
                       })

        filer.template('/etc/systemd/system/fabagent-central.service',
                       src='systemd.service',
                       data={
                           'description': 'fabagent',
                           'exec': '/opt/fabkit/bin/fabclient agent_central',
                           'user': '******',
                       })

        filer.template(
            '/etc/systemd/system/fabnode.service',
            src='systemd.service',
            data={
                'description': 'fabnode',
                'exec': '/opt/fabkit/bin/fabnode',  # noqa
                'user': '******',
            })

        sudo('systemctl daemon-reload')

        sudo('npm install -g coffee-script')
        sudo('cd {0}/fabfile/core/webapp/fabnode && '
             'npm install'.format(common_repo))
Esempio n. 23
0
    def setup(self):
        data = self.init()

        var_dir = CONF.client.package_var_dir
        tmp_dir = os.path.join(var_dir, 'tmp')
        log_dir = '/var/log/fabkit'
        common_repo = '{0}/fabkit-repo-common'.format(var_dir)
        client_repo = '{0}/fabkit-repo-client'.format(var_dir)
        server_repo = '{0}/fabkit-repo-server'.format(var_dir)

        if data['use_package']:
            Package(data['package_name']).install()
            return

        self.install_packages()
        self.python.setup()
        filer.mkdir(var_dir, owner=data['owner'])
        filer.mkdir(log_dir, owner=data['owner'])
        filer.mkdir(tmp_dir, owner=data['owner'], mode='777')

        filer.mkdir(common_repo, owner=data['owner'])
        filer.mkdir(client_repo, owner=data['owner'])
        filer.mkdir(server_repo, owner=data['owner'])

        sudo('rm -rf {0}/fabfile*'.format(tmp_dir))
        fabfile_tar_gz = os.path.join(tmp_dir, 'fabfile.tar.gz')
        scp('/tmp/fabfile.tar.gz', fabfile_tar_gz)

        sudo('rm -rf {0}/fabfile'.format(common_repo))
        sudo('cd {0} && tar xzf {1} && '
             'cp -r fabfile {2}/fabfile'.format(
                 tmp_dir, fabfile_tar_gz, common_repo))

        sudo('{0}/bin/pip install -r {1}/fabfile/requirements.txt'.format(
             CONF.client.package_prefix, common_repo))

        data['repo'] = client_repo
        filer.template('{0}/bin/fabclient'.format(CONF.client.package_prefix),
                       src='fabric.sh', data=data, mode='755')

        data['repo'] = server_repo
        filer.template('{0}/bin/fabserver'.format(CONF.client.package_prefix),
                       src='fabric.sh', data=data, mode='755')

        filer.template('{0}/bin/fabnode'.format(CONF.client.package_prefix),
                       src='fabnode.sh', data=data, mode='755')

        filer.template('/etc/systemd/system/fabagent.service',
                       src='systemd.service', data={
                           'description': 'fabagent',
                           'exec': '/opt/fabkit/bin/fabclient agent',
                           'user': '******',
                       })

        filer.template('/etc/systemd/system/fabagent-central.service',
                       src='systemd.service', data={
                           'description': 'fabagent',
                           'exec': '/opt/fabkit/bin/fabclient agent_central',
                           'user': '******',
                       })

        filer.template('/etc/systemd/system/fabnode.service',
                       src='systemd.service', data={
                           'description': 'fabnode',
                           'exec': '/opt/fabkit/bin/fabnode',  # noqa
                           'user': '******',
                       })

        sudo('systemctl daemon-reload')

        sudo('npm install -g coffee-script')
        sudo('cd {0}/fabfile/core/webapp/fabnode && '
             'npm install'.format(common_repo))
Esempio n. 24
0
def setup():
    Package('git').install()
    Package('expect').install()
    filer.mkdir(tmp_dest, mode='777')
Esempio n. 25
0
    def setup(self):
        self.install_packages()
        self.start_services().enable_services()

        repo = '/home/{0}/fabkit-repo'.format(env.user)
        filer.mkdir(repo, use_sudo=False)
        git.setup()
        git.sync('https://github.com/fabrickit/fabkit.git',
                 dest='{0}/fabfile'.format(repo))

        python = Python('/opt/fabkit')
        python.setup()
        python.install(
            requirements='{0}/fabfile/requirements.txt'.format(repo))

        run('cd {0} && /opt/fabkit/bin/fab genconfig:fabfile.ini &&'
            ' sed -i "/^\[web\]/,/^\[/s/#hostname =.*/hostname = */g" fabfile.ini'
            .format(repo))

        data = {
            'port': 80,
            'repo': repo,
            'user': env.user,
            'group': env.user,
            'python_path': python.get_site_packages(),
            'processes': 5,
            'threads': 1,
        }

        run('cd {0}/fabfile/core/webapp/ &&'
            ' /opt/fabkit/bin/python manage.py migrate &&'
            ' echo "from django.contrib.auth.models import User;'
            '       User.objects.create_superuser(\'admin\', \'admin@localhost\', \'admin\')"'
            '       | /opt/fabkit/bin/python manage.py shell &&'
            ' /opt/fabkit/bin/python manage.py collectstatic --noinput'.format(
                repo))

        sudo('chmod 755 /home/{0}'.format(env.user))

        if re.match('CentOS .*', env.node['os']):
            log_prefix = '/var/log/httpd/{0}'.format(env.user)
            data['error_log'] = '{0}-error.log'.format(log_prefix)
            data['custom_log'] = '{0}-access.log'.format(log_prefix)

            if filer.template(src='httpd.conf',
                              dest='/etc/httpd/conf.d/{0}_httpd.conf'.format(
                                  env.user),
                              data=data):
                self.handlers['restart_httpd'] = True

        elif re.match('Ubuntu .*', env.node['os']):
            log_prefix = '/var/log/apache2/{0}'.format(env.user)
            data['error_log'] = '{0}-error.log'.format(log_prefix)
            data['custom_log'] = '{0}-access.log'.format(log_prefix)

            if filer.template(
                    src='httpd.conf',
                    dest='/etc/apache2/sites-enabled/{0}_httpd.conf'.format(
                        env.user),
                    data=data):
                self.handlers['restart_apache2'] = True

        self.exec_handlers()

        # install
        sudo('npm install -g coffee-script'.format(repo))
        sudo('cd {0}/fabfile/core/webapp && npm install'.format(repo))

        # install node packages for develop
        sudo('npm install -g grunt-cli'.format(repo))
        sudo(
            'cd {0}/fabfile/core/webapp/node_chat && npm install'.format(repo))
Esempio n. 26
0
    def setup(self):
        data = self.init()

        node_os = env.node['os']
        if re.match('Ubuntu 14.*', node_os):
            with api.warn_only():
                sudo('apt-get update -y')

        self.install_packages()
        self.start_services()

        sudo('modprobe kvm')
        sudo('modprobe kvm_intel')

        for i, vm in enumerate(data['libvirt_vms']):
            template_data = {
                'user': CONF.test.user,
                'password': CONF.test.password,
                'vm': vm,
                'gateway': data['libvirt']['gateway'],
                'netmask': data['libvirt']['netmask'],
            }

            vm_dir = '/var/lib/libvirt/images/{0}'.format(vm['name'])
            image_path = '{0}/vm.img'.format(vm_dir)
            metadata_path = '{0}/meta-data'.format(vm_dir)
            userdata_path = '{0}/user-data'.format(vm_dir)
            configiso_path = '{0}/config.iso'.format(vm_dir)

            src_image = vm['src_image'].rsplit('/', 1)[1]
            src_image_path = '/var/lib/libvirt/images/{0}'.format(src_image)
            src_image_format = 'qcow2'

            if src_image_path[-3:] == '.xz':
                src_image_path = src_image_path[:-3]
                src_image_format = 'xz'

            if not filer.exists(src_image_path):
                sudo('cd /var/lib/libvirt/images/ && wget {0}'.format(vm['src_image']))

                if src_image_format == 'xz':
                    sudo('cd /var/lib/libvirt/images/ && xz -d {0}'.format(src_image))

            with api.warn_only():
                sudo("virsh list --all | grep {0} && virsh destroy {0}"
                     " && virsh undefine {0}".format(vm['name']))

            sudo('rm -rf {0}'.format(vm_dir))
            filer.mkdir(vm_dir)

            if not filer.exists(image_path):
                sudo('cp {0} {1}'.format(src_image_path, image_path))
                sudo('qemu-img resize {0} {1}G'.format(image_path, vm.get('disk_size', 10)))

            filer.template(metadata_path, src='meta-data', data=template_data)
            filer.template(userdata_path, src=vm['template'], data=template_data)
            if not filer.exists(configiso_path):
                sudo('genisoimage -o {0} -V cidata -r -J {1} {2}'.format(
                    configiso_path, metadata_path, userdata_path))

            sudo("sed -i 's/^Defaults.*requiretty/# Defaults requiretty/' /etc/sudoers")

            vm['uuid'] = str(uuid.uuid1())
            vm['image_path'] = image_path
            vm['configiso_path'] = configiso_path
            vm['tap'] = 'tap{0}'.format(i)
            vm['mac'] = self.get_random_mac()
            domain_xml = '/tmp/domain-{0}.xml'.format(vm['name'])
            filer.template(domain_xml, src='domain.xml', data=vm)

            with api.warn_only():
                sudo("virsh net-update default delete ip-dhcp-host \"`virsh net-dumpxml default | grep '{0}' | sed -e 's/^ *//'`\"".format(vm['ip']))

            sudo("virsh net-update default add ip-dhcp-host "
                 "\"<host mac='{0}' name='{1}' ip='{2}' />\"".format(
                     vm['mac'], vm['name'], vm['ip']))

            sudo('virsh define {0}'.format(domain_xml))
            sudo('virsh start {0}'.format(vm['name']))

            # sudo("virt-install"
            #      " --connect=qemu:///system"
            #      " --name={name} --vcpus={vcpus} --ram={ram}"
            #      " --accelerate --hvm --virt-type=kvm"
            #      " --cpu host"
            #      " --network bridge=virbr0,model=virtio"
            #      " --disk {image_path},format=qcow2 --import"
            #      " --disk {configiso_path},device=cdrom"
            #      " --nographics &".format(
            #          name=vm['name'],
            #          vcpus=vm['vcpus'],
            #          ram=vm['ram'],
            #          image_path=image_path,
            #          configiso_path=configiso_path,
            #          ip=vm['ip'],
            #      ), pty=False)  # ), pty=False)

        for vm in data['libvirt_vms']:
            while True:
                with api.warn_only():
                    if run('nmap -p 22 {0} | grep open'.format(vm['ip'])):
                        break
                    time.sleep(5)

        sudo("iptables -R FORWARD 1 -o virbr0 -s 0.0.0.0/0"
             " -d 192.168.122.0/255.255.255.0 -j ACCEPT")
        for vm in data['libvirt_vms']:
            for port in vm.get('ports', []):
                sudo("iptables -t nat -A PREROUTING -p tcp"
                     " --dport {0[1]} -j DNAT --to {1}:{0[0]}".format(
                         port, vm['ip']))

        for ip in data['iptables']:
            for port in ip.get('ports', []):
                sudo("iptables -t nat -A PREROUTING -p tcp"
                     " --dport {0[1]} -j DNAT --to {1}:{0[0]}".format(
                         port, ip['ip']))