Esempio n. 1
0
    def scp(self, source, target):
        """SCP a local file to a host"""

        info('Copying {} to {} on {}'.format(source, target, self.name))
        client = paramiko.client.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(self.address, username='******', password=self.password)
        sftp = client.open_sftp()
        sftp.put(source, target)
Esempio n. 2
0
    def scp(self, source, target):
        """SCP a local file to a host"""

        info('Copying {} to {} on {}'.format(source, target, self.name))
        client = paramiko.client.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(self.address, username='******', password=self.password)
        sftp = client.open_sftp()
        sftp.put(source, target)
 def a(self, fqdn, ip):
     """Add an A record"""
     host = HostManager.get(self.host)
     if host.exists():
         try:
             resolver = dns.resolver.Resolver(configure=False)
             resolver.nameservers = [host.address]
             resolver.query(fqdn, 'A')
         except dns.resolver.NXDOMAIN:
             info('Creating A record for {}: {}'.format(fqdn, ip))
             host.ssh(
                 'echo -e "server 127.0.0.1\nupdate add {} 604800 A {}\nsend" | nsupdate -k /etc/bind/rndc.key'
                 .format(fqdn, ip))
 def ptr(self, fqdn, ip):
     """Add a PTR record"""
     host = HostManager.get(self.host)
     if host.exists():
         arpa = dns.reversename.from_address(ip).to_text()
         try:
             resolver = dns.resolver.Resolver(configure=False)
             resolver.nameservers = [host.address]
             resolver.query(arpa, 'PTR')
         except dns.resolver.NXDOMAIN:
             info('Creating PTR record for {}: {}'.format(ip, arpa))
             host.ssh(
                 'echo -e "server 127.0.0.1\nupdate add {} 604800 PTR {}\nsend" | nsupdate -k /etc/bind/rndc.key'
                 .format(arpa, fqdn))
Esempio n. 5
0
def main():
    """Destroy any resources accociated with guests"""

    hypervisor = hypervisor_client.HypervisorClient('localhost')

    info('Deleting hosts ...')
    hosts = hypervisor.host_list()
    for host in hosts:
        detail(host['name'])
        hypervisor.host_delete(host['name'])

    info('Deleting networks ...')
    networks = hypervisor.network_list()
    for network in networks:
        detail(network['name'])
        hypervisor.network_delete(network['name'])
Esempio n. 6
0
    def ssh(self, command, **kwargs):
        """SSH onto a host and execute a command"""

        if 'acceptable_exitcodes' in kwargs:
            acceptable_exitcodes = kwargs['acceptable_exitcodes']
        else:
            acceptable_exitcodes = [0]

        info('Executing on {}: {}'.format(self.name, command))
        client = paramiko.client.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(self.address, username='******', password=self.password)
        channel = client.get_transport().open_session()
        channel.set_combine_stderr(True)
        channel.exec_command(command)
        while not channel.exit_status_ready():
            while channel.recv_ready():
                sys.stdout.write(channel.recv(8192))
            time.sleep(0.1)
        while channel.recv_ready():
            sys.stdout.write(channel.recv(8192))
        detail("Exited with status {}".format(channel.recv_exit_status()))
        if channel.recv_exit_status() not in acceptable_exitcodes:
            raise RuntimeError('command execution failed')
Esempio n. 7
0
    def ssh(self, command, **kwargs):
        """SSH onto a host and execute a command"""

        if 'acceptable_exitcodes' in kwargs:
            acceptable_exitcodes = kwargs['acceptable_exitcodes']
        else:
            acceptable_exitcodes = [0]

        info('Executing on {}: {}'.format(self.name, command))
        client = paramiko.client.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(self.address, username='******', password=self.password)
        channel = client.get_transport().open_session()
        channel.set_combine_stderr(True)
        channel.exec_command(command)
        while not channel.exit_status_ready():
            while channel.recv_ready():
                sys.stdout.write(channel.recv(8192))
            time.sleep(0.1)
        while channel.recv_ready():
            sys.stdout.write(channel.recv(8192))
        detail("Exited with status {}".format(channel.recv_exit_status()))
        if channel.recv_exit_status() not in acceptable_exitcodes:
            raise RuntimeError('command execution failed')
Esempio n. 8
0
    def create(self):
        """Create a host, blocking until it is provisioned and SSH is running"""

        info('Creating host {} ...'.format(self.name))
        detail('Memory  {} MB'.format(self.memory))
        detail('Disks   {} GB'.format(self.disks))
        detail('Address {}'.format(self.address))
        detail('Netmask {}'.format(self.netmask))
        detail('Gateway {}'.format(self.gateway))
        detail('DNS     {}'.format(self.nameservers))

        preseed = preseed_server_client.PreseedServerClient('localhost')
        hypervisor = hypervisor_client.HypervisorClient('localhost')

        # Create networks on the hypervisor
        info('Creating networks ...')
        network_names, network_metadata = self.create_networks(hypervisor)

        # Create a preseed entry
        metadata = {
            'root_password':
            crypt.crypt(self.password, '$6$salt'),
            'finish_url':
            'http://{}:8421/hosts/{}/finish'.format(self.gateway, self.name),
            'networks':
            network_metadata,
        }
        preseed.host_create(self.name, 'preseed.xenial.erb',
                            'finish.xenial.erb', metadata)

        # Set the preseed kernel command line parameters, mostly static network options
        extra_args = [
            'auto=true', 'priority=critical', 'vga=normal',
            'hostname={}'.format(self.name), 'domain={}'.format(self.domain),
            'url=http://{}:8421/hosts/{}/preseed'.format(
                self.gateway, self.name), 'netcfg/choose_interface=auto',
            'netcfg/disable_autoconfig=true', 'netcfg/get_ipaddress={}'.format(
                self.address), 'netcfg/get_netmask={}'.format(self.netmask),
            'netcfg/get_gateway={}'.format(self.gateway),
            'netcfg/get_nameservers="{}"'.format(' '.join(self.nameservers)),
            'netcfg/confirm_static=true'
        ]

        # Create the host on the hypervisor
        # This is a non-blocking operation, so we need to poll for completion
        info('Creating host ...')
        hypervisor.host_create(self.name, self.memory, self.disks,
                               network_names, self.location,
                               ' '.join(extra_args))
        delta = self.wait_for_state(hypervisor, 'shut off')
        detail('Host created in {}s'.format(delta))

        info('Rebooting host ...')
        hypervisor.host_start(self.name)

        # Wait for SSH to become available for provisioning
        info('Waiting for SSH ...')
        delta = self.wait_for_port(22)
        detail('Port listening in {}s'.format(delta))

        # Clean up to the preseed server
        preseed.host_delete(self.name)
Esempio n. 9
0
    def create(self):
        """Create a host, blocking until it is provisioned and SSH is running"""

        info('Creating host {} ...'.format(self.name))
        detail('Memory  {} MB'.format(self.memory))
        detail('Disks   {} GB'.format(self.disks))
        detail('Address {}'.format(self.address))
        detail('Netmask {}'.format(self.netmask))
        detail('Gateway {}'.format(self.gateway))
        detail('DNS     {}'.format(self.nameservers))

        preseed = preseed_server_client.PreseedServerClient('localhost')
        hypervisor = hypervisor_client.HypervisorClient('localhost')

        # Create networks on the hypervisor
        info('Creating networks ...')
        network_names, network_metadata = self.create_networks(hypervisor)

        # Create a preseed entry
        metadata = {
            'root_password': crypt.crypt(self.password, '$6$salt'),
            'finish_url': 'http://{}:8421/hosts/{}/finish'.format(self.gateway, self.name),
            'networks': network_metadata,
        }
        preseed.host_create(self.name, 'preseed.xenial.erb', 'finish.xenial.erb', metadata)

        # Set the preseed kernel command line parameters, mostly static network options
        extra_args = [
            'auto=true',
            'priority=critical',
            'vga=normal',
            'hostname={}'.format(self.name),
            'domain={}'.format(self.domain),
            'url=http://{}:8421/hosts/{}/preseed'.format(self.gateway, self.name),
            'netcfg/choose_interface=auto',
            'netcfg/disable_autoconfig=true',
            'netcfg/get_ipaddress={}'.format(self.address),
            'netcfg/get_netmask={}'.format(self.netmask),
            'netcfg/get_gateway={}'.format(self.gateway),
            'netcfg/get_nameservers="{}"'.format(' '.join(self.nameservers)),
            'netcfg/confirm_static=true'
        ]

        # Create the host on the hypervisor
        # This is a non-blocking operation, so we need to poll for completion
        info('Creating host ...')
        hypervisor.host_create(self.name, self.memory, self.disks, network_names,
                               self.location, ' '.join(extra_args))
        delta = self.wait_for_state(hypervisor, 'shut off')
        detail('Host created in {}s'.format(delta))

        info('Rebooting host ...')
        hypervisor.host_start(self.name)

        # Wait for SSH to become available for provisioning
        info('Waiting for SSH ...')
        delta = self.wait_for_port(22)
        detail('Port listening in {}s'.format(delta))

        # Clean up to the preseed server
        preseed.host_delete(self.name)