Beispiel #1
0
    def node(self, number):
        conn = connection(type="ssh",
                          user=self.variables.provision_user,
                          private_key=function.file(
                              self.variables.provision_ssh_key),
                          timeout=self.variables.connection_timeout)
        prov = list()
        prov.append(
            provisioner(
                "file",
                content=self.o.shared["join_cluster_as_worker"].rendered,
                destination="/tmp/join_cluster_as_worker.sh"))

        prov.append(
            provisioner("remote-exec",
                        inline=[
                            "chmod +x /tmp/join_cluster_as_worker.sh",
                            "/tmp/join_cluster_as_worker.sh {}".format(
                                self.variables.join_token)
                        ]))

        prov.append(
            provisioner("remote-exec",
                        when="destroy",
                        inline=[
                            "docker swarm leave",
                        ],
                        on_failure="continue"))

        return self.create_droplet(droplet_type="worker",
                                   number=number,
                                   conn=conn,
                                   prov=prov)
Beispiel #2
0
 def add_ssh_key_config(cls, public_key: str):
     return provisioner("remote-exec",
                        provisioner=provisioner(
                            "remote-exec",
                            inline=[
                                'mkdir -p ~/.ssh',
                                f"{public_key} >> ~/.ssh/authorized_keys"
                            ],
                        ))
Beispiel #3
0
    def create_managers(self):
        self.prepare_template()
        for i in range(self.variables.total_instances):
            droplet_manager = self.node(i + 1)

            if i == 0:
                swarm_tokens = data("external",
                                    "swarm_tokens",
                                    program=[
                                        "bash", self.curdir +
                                        "/scripts/get-swarm-join-tokens.sh"
                                    ],
                                    query={
                                        "host":
                                        droplet_manager.ipv4_address,
                                        "user":
                                        self.variables.provision_user,
                                        "private_key":
                                        self.variables.provision_ssh_key
                                    })
                self.o.shared["swarm_tokens"] = swarm_tokens
                self.o.terrascript.add(swarm_tokens)

            prov = list()
            prov.append(
                provisioner(
                    "file",
                    content=self.o.shared["provision_manager"].rendered,
                    destination="/tmp/provision-manager.sh"))

            prov.append(
                provisioner(
                    "remote-exec",
                    inline=[
                        "chmod +x /tmp/provision-manager.sh",
                        "/tmp/provision-manager.sh " +
                        droplet_manager.ipv4_address_private + " " +
                        function.lookup(self.o.shared["swarm_tokens"].result,
                                        "manager", ""),
                    ]))
            self.o.terrascript.add(
                resource("null_resource",
                         "bootstrap",
                         connection=connection(
                             type="ssh",
                             host=droplet_manager.ipv4_address,
                             user=self.variables.provision_user,
                             private_key=function.file(
                                 self.variables.provision_ssh_key),
                             timeout=self.variables.connection_timeout),
                         triggers={"cluster_instance_ids": droplet_manager.id},
                         provisioner=prov))
Beispiel #4
0
    def upload_file(
        self,
        content: str,
        *,
        destination: str = DEFAULT_UPLOAD_PATH,
        ssh_user: str = DEFAULT_SSH_USER,
        ssh_private_key: str,
        ssh_host: str,
        ssh_port: int = DEFAULT_SSH_PORT,
    ):
        upload_config = Terrascript()

        ssh_conn = self.gen_ssh_conn_config(
            ssh_user=ssh_user,
            ssh_private_key=ssh_private_key,
            ssh_host=ssh_host,
            ssh_port=ssh_port,
        )
        file_resource = self.null_resource(
            "upload_file_resource",
            provisioner=provisioner(
                self.TERRAFORM_RESOURCE_FILE,
                content=content,
                destination=destination,
                connection=ssh_conn,
            ),
        )

        upload_config += file_resource
        return upload_config
def test_issue32():
    """Issue 32: provisioner return one line with provisioner.file instead a dictionary."""

    ts = Terrascript()

    ts += provider('aws', region='ap-southeast-2')

    p = provisioner('local-exec', command='date > $(mktemp tmpXXXXXXX.terrascript)')

    ts += aws_instance('I1', ami='ami-60a26a02', instance_type='t2.nano', provisioner=p)

    j = ts.dump()

    assert 'mktemp' in j

    assert ts.validate() is True
Beispiel #6
0
    def remote_exec(self,
                    *,
                    ssh_user: str = DEFAULT_SSH_USER,
                    ssh_private_key: str,
                    ssh_host: str,
                    ssh_port: int = DEFAULT_SSH_PORT):
        exec_config = Terrascript()
        ssh_conn = self.gen_ssh_conn_config(ssh_user=ssh_user,
                                            ssh_private_key=ssh_private_key,
                                            ssh_host=ssh_host,
                                            ssh_port=ssh_port)
        exec_resource = self.null_resource('remote-exec',
                                           provisioner=provisioner(
                                               "remote-exec",
                                               inline=['ls -la'],
                                               connection=ssh_conn))

        exec_config += exec_resource
        return exec_config
Beispiel #7
0
    def create_droplet(self, droplet_type, number, conn, prov):
        number_str = self.fmt_number(number)
        droplet_name = self.fmt_name(self.variables.name, number)
        droplet_name_dns = self.fmt_name(self.variables.name, number, "-")

        volume = None
        if self.variables.persistent_volumes is not None and number <= len(
                self.variables.persistent_volumes):
            volume = self.variables.persistent_volumes[number - 1].create()

            tmpl_attach = template_file(
                "attach_volume_{}".format(droplet_name),
                template=function.file(
                    os.path.join(self.curdir, "scripts", "attach_volume.sh")),
                vars={
                    "volume_name": "/dev/sda",
                    "mount":
                    self.variables.persistent_volumes[number - 1].mount
                })
            self.o.terrascript.add(tmpl_attach)
            prov.append(
                provisioner("file",
                            content=tmpl_attach.rendered,
                            destination="/tmp/attach_volume.sh"))

            prov.append(
                provisioner("remote-exec",
                            inline=[
                                "chmod +x /tmp/attach_volume.sh",
                                "/tmp/attach_volume.sh"
                            ]))

        droplet = digitalocean_droplet(
            droplet_name,
            ssh_keys=self.variables.ssh_keys,
            image=self.variables.image,
            region=self.variables.region,
            size=self.variables.size,
            private_networking="true",
            backups=self.variables.backups,
            ipv6="false",
            user_data=self.variables.user_data,
            tags=self.get_tags_id(),
            count=1,
            name="{}.{}".format(droplet_name_dns, self.variables.domain),
            connection=conn,
            volume_ids=[volume.id] if not (volume is None) else None,
            provisioner=prov)

        self.o.shared[droplet_type + "_nodes"].append(droplet)
        self.o.terrascript.add(droplet)
        self.o.terrascript.add(
            output("{}_id".format(droplet_name),
                   value=droplet.id,
                   description="The {} node id".format(droplet_type)))
        self.o.terrascript.add(
            output("{}_ipv4_public".format(droplet_name),
                   value=droplet.ipv4_address,
                   description="The {} nodes public ipv4 address".format(
                       droplet_type)))
        self.o.terrascript.add(
            output("{}_ipv4_private".format(droplet_name),
                   value=droplet.ipv4_address_private,
                   description="The {} nodes private ipv4 address".format(
                       droplet_type)))

        if self.variables.create_dns:
            self.create_dns_entry(domain=self.variables.domain,
                                  entry=droplet_name_dns,
                                  ip=droplet.ipv4_address)
            self.create_dns_entry(domain=self.variables.domain,
                                  entry="{}-internal".format(droplet_name_dns),
                                  ip=droplet.ipv4_address_private)
            self.create_dns_entry(domain=self.variables.domain,
                                  entry=self.variables.tags[0],
                                  ip=droplet.ipv4_address,
                                  name="{}-{}".format(droplet_name_dns,
                                                      self.variables.tags[0]))

        return droplet
Beispiel #8
0
    def node(self, number):
        conn = connection(type="ssh",
                          user=self.variables.provision_user,
                          private_key=function.file(
                              self.variables.provision_ssh_key),
                          timeout=self.variables.connection_timeout)

        prov1 = provisioner(
            "file",
            content=self.o.shared["provision_first_manager"].rendered,
            destination="/tmp/provision-first-manager.sh")

        prov3 = provisioner(
            "remote-exec",
            inline=[
                "chmod +x /tmp/provision-first-manager.sh",
                "/tmp/provision-first-manager.sh ${self.ipv4_address_private}",
            ])

        prov4 = provisioner("remote-exec",
                            when="destroy",
                            inline=[
                                "timeout 25 docker swarm leave --force",
                            ],
                            on_failure="continue")

        prov = [prov4]
        if number == 1:
            prov = [prov1, prov3] + prov

        if not (self.variables.remote_api_ca is None
                or self.variables.remote_api_certificate is None
                or self.variables.remote_api_key is None):
            home_ca = "~/.docker"
            prov.append(
                provisioner("remote-exec", inline=[
                    "mkdir -p " + home_ca,
                ]))
            prov.append(
                provisioner("file",
                            content=self.variables.remote_api_ca,
                            destination=home_ca + "/ca.pem"))
            prov.append(
                provisioner("file",
                            content=self.variables.remote_api_certificate,
                            destination=home_ca + "/server-cert.pem"))
            prov.append(
                provisioner("file",
                            content=self.variables.remote_api_key,
                            destination=home_ca + "/server-key.pem"))
            prov.append(
                provisioner("file",
                            content=os.path.join(self.curdir, "scripts",
                                                 "certs", "default.sh"),
                            destination=home_ca + "install_certificates.sh"))
            prov.append(
                provisioner("remote-exec",
                            inline=[
                                "chmod +x " + home_ca +
                                "/install_certificates.sh",
                                home_ca + "/install_certificates.sh",
                            ]))

        return self.create_droplet(droplet_type="manager",
                                   number=number,
                                   conn=conn,
                                   prov=prov)