Beispiel #1
0
    def __init__(self, token, name, servertype, snapshot, location, volume):
        self.name = name
        self.servertype = servertype
        self.client = Client(token=token)

        # get id from snapshot
        images = self.client.images.get_all()
        for i in images:
            if i.data_model.description == snapshot:
                imageId = i.data_model.id
        self.snapshot = imageId

        # get id from location
        locations = self.client.locations.get_all()
        for i in locations:
            if i.data_model.name == location:
                locationId = i.data_model.id
        self.location = locationId

        # get id from volume
        volumes = self.client.volumes.get_all()
        for i in volumes:
            if i.data_model.name == volume:
                if i.data_model.location.data_model.id == self.location:
                    volumeId = i.data_model.id
        self.volume = volumeId

        # check for running server
        servers = self.client.servers.get_all()
        self.server = None
        for i in servers:
            if i.data_model.name == name:
                self.server = i.data_model
                self.running = True
                print("existing server " + self.server.status)
def main():
    args = parse_args()
    loaded = load_vault('misc/vault_hetzner.yml')
    client = Client(token=loaded["hetzner_cloud_api_key"])

    if args.list:
        hosts = list_running_hosts(client=client)
        json.dump({'hcloud': hosts}, sys.stdout)
    else:
        details = get_host_details(client, args.host)
        json.dump(details, sys.stdout)
def delete():
    with open(data_file, "r") as file:
        data = json.load(file)

    client = Client(token=data["token"])
    response = client.servers.delete(server=Server(id=data["server_id"]), )

    data["server_id"] = "SERVER-IS-DOWN"

    with open(data_file, "w") as file:
        json.dump(data, file, indent=4)
    click.echo("deletion complete")
Beispiel #4
0
def main():
    config = read_config()
    token = config['hcloud']['token']

    client = Client(token)
    all_servers = client.servers.get_all()
    filters = []
    if config.has_section('filters'):
        filters = config.items('filters')

    servers = []
    for server in all_servers:
        if matches(filters, server):
            servers.append(server)

    groups_prefix = 'groups:'
    configured_groups = [
        section for section in config.sections()
        if section.startswith(groups_prefix)
    ]
    group_filters = {
        group_section[len(groups_prefix):]: config.items(group_section)
        for group_section in configured_groups
    }

    server_info = dict([hostvars(server) for server in servers])

    ungrouped = set(server_info.keys())

    groups = {}
    for group, selectors in group_filters.items():
        groups[group] = []
        for server in servers:
            if matches(selectors, server):
                name = clean_name(server.data_model.name)
                groups[group].append(name)
                ungrouped.discard(name)

    inventory = {
        '_meta': {
            'hostvars': server_info
        },
        "all": {
            "children": list(groups.keys()) + ["ungrouped"]
        },
        "ungrouped": {
            "hosts": list(server_info.keys())
        }
    }
    for group, hosts in groups.items():
        inventory[group] = {'hosts': hosts}

    json.dump(inventory, sys.stdout)
    def do_POST(self):
        content_length = 0
        try:
            content_length = int(
                self.headers['Content-Length'])  # <--- Gets the size of data
        except:
            message = "Warning, content-length was wrong. We correct it to zero"  # TODO don't correct like that, handle it properly
            print('\033[93m' + "[-] " + message + '\033[0m')
            content_length = 0
        post_data = self.rfile.read(
            content_length)  # <--- Gets the data itself
        logging.info("POST request,\nPath: %s\nHeaders:\n%s\n\nBody:\n%s\n",
                     str(self.path), str(self.headers),
                     post_data.decode('utf-8'))

        body = post_data.decode('utf-8')
        position = body.find('hostname=')
        hostname = body[position + len("hostname="):]
        position = hostname.find('&')
        hostname = hostname[:position]
        if db_check_if_id_exists(self.path[1:]):
            if check_if_file_exists("parts/" + hostname):
                print(str(self.headers["Host"]))
                user = "******"
                ip = self.client_address[0]
                scp_path = "/root/results/"
                scp_dest = "./results/"
                p = subprocess.Popen([
                    "scp", "-o", "StrictHostKeyChecking=no", "-o",
                    "UserKnownHostsFile=/dev/null", "-r",
                    user + "@" + ip + ":" + scp_path, scp_dest
                ])
                sts = os.waitpid(p.pid, 0)
                print("Copy done")
                client = Client(
                    token=configVars["hetzner_cloud_api_key"]
                )  # Please paste your API token here between the quotes
                servers = client.servers.get_all()
                for server in servers:
                    print("Found server: " + server.public_net.ipv4.ip)
                    if server.public_net.ipv4.ip == ip:
                        client.servers.delete(server)
                        break
                print("Done")  # TODO also set active status to zero in DB
        else:
            print("Got message for part_id that does not exist")

        self._set_response()
        self.wfile.write(b"")
Beispiel #6
0
    def get_client(self) -> Client:
        """
        Generic method to get or create a Hetzner Cloud client.
        """
        if self._client:
            return self._client

        new_api_token = self.api_token or os.environ.get("HCLOUD_API_TOKEN")
        if new_api_token is not None:
            self.api_token = new_api_token

        if self.api_token is None:
            raise Exception("please set ‘apiToken’ or $HCLOUD_API_TOKEN")

        self._client = Client(token=self.api_token)
        return self._client
def create():
    with open(data_file, "r") as file:
        data = json.load(file)

    client = Client(token=data["token"])
    response = client.servers.create("win10-OCR",
                                     server_type=ServerType(name="cx41"),
                                     image=Image(id=22859215),
                                     networks=[Network(id=135205)],
                                     location=Location(id=2))
    server = response.server
    data["server_id"] = f"{server.id}"

    with open(data_file, "w") as file:
        json.dump(data, file, indent=4)
    click.echo("creation complete")
Beispiel #8
0
    async def from_new(
        cls,
        prefix: str = PREFIX,
        tokenvar: str = TOKENVAR,
        wait: float = WAIT,
        dask_ipc: int = DASK_IPC,
        dask_dash: int = DASK_DASH,
        dask_nanny: int = DASK_NANNY,
        log: Union[Logger, None] = None,
        **kwargs,
    ) -> ClusterABC:
        """
        Creates a new cluster
        """

        log = getLogger(name = prefix) if log is None else log

        log.info('Creating cloud client ...')
        client = Client(token = os.environ[tokenvar])

        creator = await Creator.from_async(
            client = client,
            prefix = prefix,
            fn_public = cls._fn_public(prefix),
            fn_private = cls._fn_private(prefix),
            wait = wait,
            dask_ipc = dask_ipc,
            dask_dash = dask_dash,
            dask_nanny = dask_nanny,
            log = log,
            **kwargs,
        )

        return cls(
            client = client,
            scheduler = creator.scheduler,
            workers = creator.workers,
            network = creator.network,
            firewall = creator.firewall,
            dask_ipc = dask_ipc,
            dask_dash = dask_dash,
            dask_nanny = dask_nanny,
            prefix = prefix,
            wait = wait,
            log = log,
        )
Beispiel #9
0
    def get_client(self) -> Client:
        """
        Generic method to get or create a Hetzner Cloud client.
        """

        if hasattr(self, "_client"):
            if self._client:
                return self._client

        new_api_token = (
            self.get_defn().config.apiToken
            if self.name in (self.depl.definitions or [])
            else None  # type: ignore
        ) or os.environ.get("HCLOUD_API_TOKEN", None)

        if new_api_token is not None:
            self.api_token = new_api_token

        if self.api_token is None:
            raise Exception("please set ‘apiToken’ or $HCLOUD_API_TOKEN")

        self._client = Client(token=self.api_token)
        return self._client
def create_servers(size=SIZE, total=PERFORMERS):
    """create some servers"""

    # get the last server number
    servers = get_servers()
    if len(servers) > 0:
        names = [s.name for s in servers]
        nums = sorted([int(n.split("-")[-1]) for n in names], reverse=True)
        start = nums[0] + 1
    else:
        start = 0

    client = Client(token=TOKEN)
    key = get_key()
    image = get_image()
    st = ServerType(SIZE)

    for i in range(start, start + total):
        name = f"{NAME}-{i}"
        response = client.servers.create(name=name,
                                         server_type=st,
                                         image=image,
                                         ssh_keys=[key])
        print(response)
Beispiel #11
0
from hcloud import Client
from hcloud.images.domain import Image
from hcloud.server_types.domain import ServerType

client = Client(token="{YOUR_API_TOKEN}"
                )  # Please paste your API token here between the quotes
response = client.servers.create(name="my-server",
                                 server_type=ServerType("cx11"),
                                 image=Image(name="ubuntu-20.04"))
server = response.server
print(server)
print("Root Password" + response.root_password)
Beispiel #12
0
def hetzner_client():
    client = Client(token="token")
    patcher = mock.patch.object(client, "request")
    patcher.start()
    yield client
    patcher.stop()
Beispiel #13
0
 def __init__(self):
     self.hcloud = Client(token=os.getenv("HCLOUD_API_TOKEN"))
Beispiel #14
0
from hcloud import Client
from hcloud.images.domain import Image
from hcloud.server_types.domain import ServerType

# Create a client
client = Client(token="project-token")

# Create 2 servers
# Create 2 servers
response1 = client.servers.create("Server1",
                                  server_type=ServerType(name="cx11"),
                                  image=Image(id=4711))

response2 = client.servers.create("Server2",
                                  server_type=ServerType(name="cx11"),
                                  image=Image(id=4711))
# Get all servers
server1 = response1.server
server2 = response2.server

servers = client.servers.get_all()

assert servers[0].id == server1.id
assert servers[1].id == server2.id
# Create 2 volumes

response1 = client.volumes.create(size=15,
                                  name="Volume1",
                                  location=server1.location)
response2 = client.volumes.create(size=10,
                                  name="Volume2",
Beispiel #15
0
 def client(self):
     Client._version = '0.0.0'
     return Client(token="project_token")
Beispiel #16
0
def hetzner_client():
    hetzner_client = Client(token="test-token",
                            api_endpoint=os.getenv(
                                "FAKE_API_ENDPOINT",
                                default="http://localhost:4000/v1"))
    return hetzner_client
Beispiel #17
0
 def __init__(self, config):
     super().__init__(max_nodes=config.getint('clouds', 'hetzner_max_nodes', fallback=None))
     self.client = Client(token=config.get('clouds', 'hetzner_token'))
     self.config = config
Beispiel #18
0
from hcloud import Client

from hcloud.images.domain import Image
from hcloud.server_types.domain import ServerType
from hcloud.ssh_keys.client import SSHKeysClient

from ctrldbops import add_hetzner, get_hetzner

import configparser

hetzner_config = configparser.ConfigParser()
hetzner_config.read('/home/bitclouds/bitclouds/controller/config.ini')

hetzner_token = hetzner_config['hetzner']['api_key']

client = Client(
    token=hetzner_token)  # Please paste your API token here between the quotes
sshClient = SSHKeysClient(client)


def getServers():
    hetzner_servers = client.servers.get_all()

    servers = list()

    for server in hetzner_servers:
        serverData = dict()

        serverData['id'] = server.id
        serverData['ip'] = server.public_net.ipv4.ip
        serverData['name'] = server.name
Beispiel #19
0
    def client(self):
        Client._version = '0.0.0'
        client = Client(token="project_token")

        client._requests_session = MagicMock()
        return client
Beispiel #20
0
 def test__get_user_agent_with_application_name_and_version(self, client):
     client = Client(token="project_token",
                     application_name="my-app",
                     application_version="1.0.0")
     user_agent = client._get_user_agent()
     assert user_agent == "my-app/1.0.0 hcloud-python/0.0.0"
from hcloud import Client
from hcloud.images.domain import Image
from hcloud.server_types.domain import ServerType
from hcloud.servers.domain import Server


client = Client(token="......")
serverName = "test" 


oldServer = client.servers.get_by_name(name=serverName)

#Shut down the OldServer
try:
    oldServer.power_off()
except:
    print("Error when turning down the server. Confirm if the server's name is right")
    quit()


#Get all the information of the old server
oldServerType = (Server(id=oldServer).id).server_type
oldServerDatacenter = (Server(id=oldServer).id).datacenter
oldServerVolumes = (Server(id=oldServer).id).volumes
oldServerFloatingIP = ((Server(id=oldServer).id).public_net).floating_ips

sshKeys = client.ssh_keys.get_all()

#Find the newest backup
images = client.images.get_all(type="backup")
backupNewestID = 0
def get_servers():
    """ Retrieve all servers """
    client = Client(token=TOKEN)
    servers = client.servers.get_all()
    servers = [s for s in servers if NAME in s.name]
    return servers
def get_key():
    """ Gets the most recent synthetic messenger image"""
    client = Client(token=TOKEN)
    keys = client.ssh_keys.get_list()
    return keys[0][0]
def get_image():
    """ Gets the most recent synthetic messenger image"""
    client = Client(token=TOKEN)
    images = client.images.get_all()
    images = [i for i in images if "synthetic" in i.description]
    return images[-1]
Beispiel #25
0
 def find():
     """Discover instances in Hetzner cloud."""
     config = Container.config.discovery
     client = Client(token=config.token())
     servers = client.servers.get_all(status="running")
     for server in servers:
         yield {
             "created":
             str(server.data_model.created),
             "datacenter":
             Hetzner.get_model_data(
                 Datacenter,
                 server.data_model.datacenter.data_model,
             ),
             "id":
             str(server.data_model.id),
             "included_traffic":
             server.data_model.included_traffic,
             "ingoing_traffic":
             server.data_model.ingoing_traffic,
             "image":
             Hetzner.get_model_data(
                 Image,
                 server.data_model.image.data_model,
             ),
             "labels":
             server.data_model.labels,
             "locked":
             server.data_model.locked,
             "name":
             server.data_model.name,
             "outgoing_traffic":
             server.data_model.outgoing_traffic,
             "private_net":
             server.data_model.private_net,
             "protection":
             server.data_model.protection,
             "public_net": {
                 "floating_ips":
                 server.data_model.public_net.floating_ips,
                 "ipv4":
                 Hetzner.get_model_data(
                     IPv4Address,
                     server.data_model.public_net.ipv4,
                 ),
                 "ipv6":
                 Hetzner.get_model_data(
                     IPv6Network,
                     server.data_model.public_net.ipv6,
                 ),
             },
             "rescue_enabled":
             server.data_model.rescue_enabled,
             "server_type":
             Hetzner.get_model_data(
                 ServerType,
                 server.data_model.server_type.data_model,
             ),
             "status":
             server.data_model.status,
         }
Beispiel #26
0
                    default=used_for,
                    help="label with purpose of machine. Default: " + used_for)
parser.add_argument('PARAM', nargs='*', help="optional parameters")
args = parser.parse_args()
NAME = args.name
if args.unique:
    NAME = args.unique

used_for = args.used_for
if not NAME: NAME = args.image
NAME = NAME.translate({
    ord('.'): ord('-'),
    ord('_'): ord('-')
})  # avoid _ and . in name. Always

client = Client(token=hcloud_api_token)

if debug:
    # list all servers in this project
    for s in client.servers.get_all():
        sd = s.data_model
        print("%-12s" % sd.name,
              "%-15s" % sd.public_net.ipv4.ip if sd.public_net else None,
              sd.server_type.data_model.name,
              sd.image.name if sd.image else None,
              sd.created,
              sd.status,
              sd.labels,
              file=sys.stderr)

    # list all ssh-keys in this project
Beispiel #27
0
    def _login(self):
        api_token = self.config['api_token']

        logging.info("logging in hcloud={}".format(self.name))
        self.client = Client(token=api_token)
        return True
Beispiel #28
0
 def __init__(self, api_token):
     self.client = Client(token=api_token)
     self.logger = logging.getLogger('vm_operator')
Beispiel #29
0
 def __init__(self) -> None:
     super().__init__()
     self.hcloud = Client(token=HCLOUD_API_TOKEN)
                       ''.join(str(e) for e in input_list_part))
            input_list_parts.append(input_list_part)
            input_list = input_list[splits:]
        MAX_PARTS = 9
        if len(input_list_parts) > MAX_PARTS:
            handle_error(
                "To many parts. Max parts {}, Current parts: {}".format(
                    MAX_PARTS, len(input_list_parts)), None, True)
        # For every part we need to create a machine
        # Upload the part into the machine
        # Wait till machine is completely initialized
        # Run whatever script we want to run (with input_part_list as parameter e.g. nmap)
        # Wait till nmap is done and get the list (maybe with webhooks?) (we are able to call a URL as soon as cloud-init is done!)

        client = Client(
            token=configVars["hetzner_cloud_api_key"]
        )  # Please paste your API token here between the quotes
        user_data_template = read_file(configVars["cloud_init_path"])

        import socket
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(("8.8.8.8", 80))
        host_ip = s.getsockname()[0]
        s.close()

        for part_id in part_ids:
            user_data_part = user_data_template
            user_data_part = user_data_part.replace("<host_ip>", host_ip)
            user_data_part = user_data_part.replace("<part_id>", part_id)

            response = client.servers.create(