1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
from pathlib import Path
from typing import Optional
import pulumi
import pulumi_digitalocean as do
import pulumi_command as command
from .ssh import SshKey
class Instance(pulumi.ComponentResource):
def __init__(
self,
name: str,
region: str,
size: str,
dns_name: str,
reserved_ip: Optional[str] = None,
volume_name: Optional[str] = None,
opts: Optional[pulumi.ResourceOptions] = None,
):
self.name = name
self.dns_name = dns_name
self.reserved_ip = reserved_ip
super().__init__(
f"{pulumi.get_project()}:instance:Instance/{name}", name, None, opts
)
ssh_key = SshKey(
f"{self.name}-ssh-keypair",
opts=pulumi.ResourceOptions(parent=self),
)
ssh = do.SshKey(
self.name,
public_key=ssh_key.public_key,
name=f"{pulumi.get_project()}-{pulumi.get_stack()}-{self.name}",
opts=pulumi.ResourceOptions(parent=self),
)
volumes = []
if volume_name is not None:
volume = do.get_volume_output(
name=volume_name,
region=region,
opts=pulumi.InvokeOptions(parent=self),
)
volumes.append(volume.id)
self.instance = do.Droplet(
self.name,
name=dns_name,
image="debian-10-x64",
region=region,
size=size,
ssh_keys=[ssh.id],
volume_ids=volumes,
opts=pulumi.ResourceOptions(parent=self),
)
if reserved_ip is not None:
self.ip_assignment = do.ReservedIpAssignment(
self.name,
ip_address=reserved_ip,
droplet_id=self.instance.id.apply(lambda id: int(id)),
opts=pulumi.ResourceOptions(parent=self),
)
connection = command.remote.ConnectionArgs(
host=self.instance.ipv4_address,
private_key=ssh_key.private_key,
user="root",
dial_error_limit=100,
)
bootstrap_debian_file = command.remote.CopyFile(
f"{self.name}-bootstrap_debian",
connection=connection,
local_path="bootstrap/debian",
remote_path="/tmp/bootstrap",
opts=pulumi.ResourceOptions(parent=self),
)
bootstrap_debian = command.remote.Command(
f"{self.name}-bootstrap_debian",
connection=connection,
create="sh /tmp/bootstrap",
opts=pulumi.ResourceOptions(
parent=self, depends_on=[bootstrap_debian_file]
),
)
sleep = command.local.Command(
f"{self.name}-sleep",
create="sleep 30",
opts=pulumi.ResourceOptions(
parent=self, depends_on=[bootstrap_debian]
),
)
make_secrets_dir = command.remote.Command(
f"{self.name}-make_secrets_dir",
connection=connection,
create="mkdir /tmp/secrets",
opts=pulumi.ResourceOptions(parent=self, depends_on=[sleep]),
)
bootstrap_arch_file = command.remote.CopyFile(
f"{self.name}-bootstrap_arch",
connection=connection,
local_path="bootstrap/arch",
remote_path="/tmp/bootstrap",
opts=pulumi.ResourceOptions(parent=self, depends_on=[sleep]),
)
secret_files = []
for file in Path(f"/mnt/puppet/{name}").glob("*"):
secret_files.append(
command.remote.CopyFile(
f"{self.name}-secret-{file.name}",
connection=connection,
local_path=str(file),
remote_path=f"/tmp/secrets/{file.name}",
opts=pulumi.ResourceOptions(
parent=self, depends_on=[make_secrets_dir]
),
)
)
command.remote.Command(
f"{self.name}-bootstrap_arch",
connection=connection,
create="sh /tmp/bootstrap",
opts=pulumi.ResourceOptions(
parent=self,
depends_on=[bootstrap_arch_file, *secret_files],
),
)
self.register_outputs(
{
"dns_name": dns_name,
"ip_address": self.instance.ipv4_address,
}
)
|