-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrender_templates.js
151 lines (145 loc) · 6.41 KB
/
render_templates.js
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/node
'use strict';
const fs = require('fs');
const util = require('util');
const writeFile = util.promisify(fs.writeFile);
const { listConfig } = require('./lib/config');
const getter = (config, key) => config[key] && config[key].h1 ? config[key].h1 : config[key];
const render_templates = config => {
const chroot_mounts = [
['proc', 'proc', '/proc'],
['sysfs', 'sysfs', '/sys'],
['bind', '/dev', '/dev'],
['devpts', 'devpts', '/dev/pts'],
['binfmt_misc', 'binfmt_misc', '/proc/sys/fs/binfmt_misc'],
];
if (config.selinux === '1') {
// Extra selinuxfs for SELinux & 'fixfiles' command
chroot_mounts.push(['selinuxfs', 'none', '/sys/fs/selinux']);
}
const tags = {
arch: getter(config, 'arch'),
distro: getter(config, 'distro'),
release: getter(config, 'version'),
edition: getter(config, 'edition'),
codename: getter(config, 'codename'),
recommended: { disk: { size: 20 } },
};
const post_mount_commands = [
'mkdir -p {{.MountPath}}/boot/efi',
'mount -t vfat {{.Device}}1 {{.MountPath}}/boot/efi',
'wget -nv {{user `download_url`}} -O {{user `download_path`}}',
'mkdir {{user `mount_qcow_path`}}',
'LIBGUESTFS_BACKEND=direct guestmount -a {{user `download_path`}} -m {{user `qcow_part`}} --ro {{user `mount_qcow_path`}}',
'setenforce 0',
];
if (getter(config, 'selinux') === '1') {
post_mount_commands.push(
'rsync -aH -X --inplace -W --numeric-ids -A -v {{user `mount_qcow_path`}}/ {{.MountPath}}/ | pv -l -c -n >/dev/null'
);
} else {
post_mount_commands.push(
'rsync -aH --inplace -W --numeric-ids -A -v {{user `mount_qcow_path`}}/ {{.MountPath}}/ | pv -l -c -n >/dev/null'
);
}
return {
variables: {
source_image: getter(config, 'source_image') || 'fedora:32',
download_path: '/home/guru/image-{{timestamp}}.qcow',
mount_qcow_path: '/home/guru/qcow-{{timestamp}}',
download_url: getter(config, 'download_url'),
qcow_part: getter(config, 'qcow_part'),
root_fs: getter(config, 'root_fs') || 'ext4',
root_fs_opts: '-E lazy_itable_init=1',
scripts: getter(config, 'custom_scripts').join(','),
...getter(config, 'cloud_init_install') ? {
cloud_init_tmp_path: '/tmp/cloud_init.py',
cloud_init_ds_src: getter(config, 'cloud_init_ds_src') || './resources/cloud-init/ds_v2/DataSourceRbxCloud.py',
} : {},
disk_size: getter(config, 'disk_size') || '10',
image_name: getter(config, 'pname'),
ssh_name: 'my-ssh',
image_description: JSON.stringify(tags),
public_netadp_service: 'public',
vm_type: 'a1.small',
repository: 'https://packages.hyperone.cloud',
cli_package: 'h1-cli',
scope_name: 'HyperOne',
state_timeout: '10m',
},
builders: [
{
type: 'hyperone',
disk_size: 10,
chroot_disk: true,
mount_partition: 4,
vm_name: `packer-${getter(config, 'name')}`,
source_image: '{{user `source_image`}}',
vm_type: '{{user `vm_type`}}',
ssh_keys: '{{user `ssh_name`}}',
chroot_command_wrapper: 'sudo {{.Command}}',
chroot_disk_size: '{{user `disk_size`}}',
image_name: '{{user `image_name`}}',
image_description: '{{user `image_description`}}',
public_netadp_service: '{{user `public_netadp_service`}}',
state_timeout: '{{user `state_timeout`}}',
pre_mount_commands: [
"[ ! -e '/etc/rpm/macros.dist' ] || sudo yum install -y \"https://dl.fedoraproject.org/pub/epel/epel-release-latest-$(awk '/rhel/ {print $2}' /etc/rpm/macros.dist).noarch.rpm\"",
'yum install -y --setopt=skip_missing_names_on_install=False mtools libgcrypt libguestfs-tools dosfstools libguestfs-xfs wget pv',
'modprobe kvm',
'sgdisk -Z {{.Device}}',
'sgdisk -n 1:0:+50MB -t 1:EF01 -c 1:EFI {{.Device}}',
'sgdisk -n 2:0:+50MB -t 2:0700 -c 2:CLOUDMD {{.Device}}',
'sgdisk -n 3:0:+1MB -t 3:EF02 -c 3:BIOS {{.Device}}',
'sgdisk -n 4:0:-0 -t 4:8300 -c 4:ROOT {{.Device}} -A 4:set:2',
'gdisk -l {{.Device}}',
'sleep 2',
'mkfs.fat {{.Device}}1 -n EFI',
'mkfs.fat {{.Device}}2 -n CLOUDMD',
'mkfs.{{user `root_fs`}} {{user `root_fs_opts`}} {{.Device}}4',
],
chroot_mounts,
post_mount_commands,
},
],
provisioners: [
{
type: 'shell',
scripts: '{{user `scripts`}}',
environment_vars: [
'SCOPE_NAME={{user `scope_name`}}',
'REPOSITORY={{user `repository`}}',
'CLI_PACKAGE={{user `cli_package`}}',
],
},
...getter(config, 'cloud_init_install') ? [
{
type: 'file',
source: '{{user `cloud_init_ds_src`}}',
destination: '{{user `cloud_init_tmp_path`}}',
},
{
type: 'shell',
inline: [
'CLOUD_INIT_DS_DIR=$(find /usr -name cloudinit -type d)',
'echo Found cloud-init in path: ${CLOUD_INIT_DS_DIR}',
'mv {{user `cloud_init_tmp_path`}} ${CLOUD_INIT_DS_DIR}/sources/DataSourceRbxCloud.py',
],
},
] : [],
],
};
};
const main = async () => {
for (const imageConfig of await listConfig(['packer'])) {
if (imageConfig.format !== 'qcow') {
console.log(`Invalid format: ${imageConfig.pname}`);
continue;
}
console.log(`Rendering: ${imageConfig.pname}`);
const template = render_templates(imageConfig);
const output_content = JSON.stringify(template, null, 4);
await writeFile(imageConfig.template_file, output_content);
}
};
main().catch(console.error);