예제 #1
0
 def get(self, localfile, remotefile):
     LOG.debug("Copying {}@{}:{} to {}".format(self.username, self.fqdn,
                                               remotefile, localfile))
     Scp.get_file(self.address,
                  localfile,
                  remotefile,
                  user=self.username,
                  password=self.password)
예제 #2
0
def main():
    sah_user = "******"
    args = parse_arguments(sah_user)

    LoggingHelper.configure_logging(args.logging_level,
                                    noisy_logger="paramiko")

    sah_password = args.password
    if not sah_password:
        sah_password = getpass("Enter the password for the "
                               "{} user of the SAH node: ".format(sah_user))

    management_net = NetworkHelper.get_management_network()

    dhcp_conf = os.path.join(os.path.expanduser('~'), 'pilot', 'dhcpd.conf')
    LOG.info("Creating dhcp configuration file {}".format(dhcp_conf))
    dhcp_conf_template = os.path.join(os.path.expanduser('~'), 'pilot',
                                      'templates', 'dhcpd.conf')

    try:
        in_file = open(dhcp_conf_template, 'r')
        file_text = in_file.read()
    except IOError:
        LOG.exception("Could not open dhcp.conf template file {}".format(
                      dhcp_conf_template))
        sys.exit(1)

    token_map = {}
    token_map["SUBNET"] = str(management_net.network)
    token_map["NETMASK"] = str(management_net.netmask)
    token_map["BROADCAST"] = str(management_net.broadcast)
    token_map["GATEWAY"] = NetworkHelper.get_management_network_gateway()

    for token in token_map.keys():
        file_text = file_text.replace(token, token_map[token])

    # Get the management network pools
    management_net_pools = NetworkHelper.get_management_network_pools()

    # Plug in the management pool ranges
    range_lines = ""
    for pool in management_net_pools:
        range_lines += "		range {} {};\n".format(
            pool["start"], pool["end"])

    file_text = re.sub("[ \t]*range[ \t]+POOL_START[ \t]+POOL_END;\n",
                       range_lines, file_text)

    try:
        with open(dhcp_conf, 'w') as out_file:
            out_file.write(file_text)
    except IOError:
        LOG.exception("Could not open {} for writing.".format(dhcp_conf))
        sys.exit(1)

    # scp dhcp.conf to the SAH
    dest_dhcp_conf = "/etc/dhcp/dhcpd.conf"
    LOG.info("Copying {} to {}@{}:{}".format(dhcp_conf, sah_user, args.sah_ip,
             dest_dhcp_conf))
    Scp.put_file(args.sah_ip, dhcp_conf, dest_dhcp_conf,
                 user=sah_user, password=sah_password)

    # The dhcp service will not start without an existing leases file,
    # so touch it to make sure it exists before starting the service
    dhcp_leases = "/var/lib/dhcpd/dhcpd.leases"
    LOG.info("Touching {}:{} as {}".format(args.sah_ip, dhcp_leases, sah_user))
    exit_code, _, std_err = Ssh.execute_command(
        args.sah_ip,
        "touch " + dhcp_leases,
        user=sah_user,
        password=sah_password)
    if exit_code != 0:
        LOG.error("Unable to touch {}:{}: {}".format(args.sah_ip,
                                                     dhcp_leases,
                                                     std_err))
        sys.exit(1)

    # Enable and restart the dhcp server on the SAH
    LOG.info("Enabling dhcpd on {} as {}".format(args.sah_ip, sah_user))
    exit_code, _, std_err = Ssh.execute_command(
        args.sah_ip,
        "systemctl enable dhcpd",
        user=sah_user,
        password=sah_password)
    if exit_code != 0:
        LOG.error("Unable to enable dhcpd on {}: {}".format(args.sah_ip,
                                                            std_err))
        sys.exit(1)

    LOG.info("Restarting dhcpd on {} as {}".format(args.sah_ip, sah_user))
    exit_code, _, std_err = Ssh.execute_command(
        args.sah_ip,
        "systemctl restart dhcpd",
        user=sah_user,
        password=sah_password)
    if exit_code != 0:
        LOG.error("Unable to restart dhcpd on {}: {}".format(args.sah_ip,
                                                             std_err))
        sys.exit(1)