Esempio n. 1
0
    def _get_server_from_main_robot(self, ip, defn=None):
        """
        Fetch the server instance using the main robot user and passwords
        from the MachineDefinition passed by 'defn'. If the definition does not
        contain these credentials or is None, it is tried to fetch it from
        environment variables.
        """
        if defn is not None and len(defn.robot_user) > 0:
            robot_user = defn.robot_user
        else:
            robot_user = os.environ.get('HETZNER_ROBOT_USER', None)

        if defn is not None and len(defn.robot_pass) > 0:
            robot_pass = defn.robot_pass
        else:
            robot_pass = os.environ.get('HETZNER_ROBOT_PASS', None)

        if robot_user is None:
            raise Exception("please either set ‘deployment.hetzner.robotUser’"
                            " or $HETZNER_ROBOT_USER for machine"
                            " ‘{0}’".format(self.name))
        elif robot_pass is None:
            raise Exception("please either set ‘deployment.hetzner.robotPass’"
                            " or $HETZNER_ROBOT_PASS for machine"
                            " ‘{0}’".format(self.name))

        if TEST_MODE:
            return TestModeServer()

        robot = Robot(robot_user, robot_pass)
        return robot.servers.get(ip)
Esempio n. 2
0
def hetzner(c,config=CFG,hosts=HOSTS):
    print("Fetching Hetzner Server")
    cfg = configparser.ConfigParser()
    cfg.read(config)

    inventory = configparser.ConfigParser(allow_no_value=True,delimiters=(' '))
    inventory.read(hosts)

    robot = Robot(cfg['hetzner']['username'], cfg['hetzner']['password'])
    configuration = HetznerCloudClientConfiguration().with_api_key(cfg['hetzner']['token']).with_api_version(1)
    client = HetznerCloudClient(configuration)

    #all_servers = client.servers().get_all() # gets all the servers as a generator
    all_servers_list = list(client.servers().get_all()) # gets all the servers as a list

    for server in all_servers_list:
        inventory[HETZNER_CLOUD][server.name]="ansible_host=%s ansible_user=%s"%(server.public_net_ipv4,cfg['ansible']['username'])
        update_dns(server.name,server.public_net_ipv4)
        #update_cloudflare(DOMAIN,server.name,server.public_net_ipv4)

    print("Fetching Hetzner Cloud")
    for server in robot.servers:
        inventory[HETZNER_SERVER][server.name]="ansible_host=%s ansible_user=%s"%(server.ip,cfg['ansible']['username'])
        update_dns(server.name,server.ip)
       # update_cloudflare(DOMAIN,server.name,server.ip)

    with open(hosts, 'w') as configfile:
        inventory.write(configfile,space_around_delimiters=False)
Esempio n. 3
0
    def _get_server_from_main_robot(self, ip, defn=None):
        (robot_user, robot_pass) = self._get_robot_user_and_pass(defn=defn)

        if TEST_MODE:
            return TestModeServer()

        robot = Robot(robot_user, robot_pass)
        return robot.servers.get(ip)
Esempio n. 4
0
    def connect(self):
        """
        Connect to the Hetzner robot by using the admin credetials in
        'self.robot_admin_user' and 'self.robot_admin_pass'.
        """
        if self._robot is not None:
            return self._robot

        self._robot = Robot(self.robot_admin_user, self.robot_admin_pass)
        return self._robot