🔧 Generate login.tsv file for card generation

This commit is contained in:
Jérôme Petazzoni
2024-11-19 00:14:02 -06:00
parent 7d35bacbbe
commit 7ee8c00cfa
4 changed files with 64 additions and 37 deletions

View File

@@ -21,18 +21,18 @@ _cmd_cards() {
die FIXME
# This will process ips.txt to generate two files: ips.pdf and ips.html
# This will process login.tsv to generate two files: cards.pdf and cards.html
(
cd tags/$TAG
../../../lib/ips-txt-to-html.py settings.yaml
../../../lib/make-login-cards.py settings.yaml
)
ln -sf ../tags/$TAG/ips.html www/$TAG.html
ln -sf ../tags/$TAG/ips.pdf www/$TAG.pdf
ln -sf ../tags/$TAG/cards.html www/$TAG.html
ln -sf ../tags/$TAG/cards.pdf www/$TAG.pdf
info "Cards created. You can view them with:"
info "xdg-open tags/$TAG/ips.html tags/$TAG/ips.pdf (on Linux)"
info "open tags/$TAG/ips.html (on macOS)"
info "xdg-open tags/$TAG/cards.html tags/$TAG/cards.pdf (on Linux)"
info "open tags/$TAG/cards.html (on macOS)"
info "Or you can start a web server with:"
info "$0 www"
}
@@ -257,7 +257,9 @@ _cmd_create() {
terraform init
echo tag = \"$TAG\" >> terraform.tfvars
echo how_many_clusters = $STUDENTS >> terraform.tfvars
echo nodes_per_cluster = $CLUSTERSIZE >> terraform.tfvars
if [ "$CLUSTERSIZE" ]; then
echo nodes_per_cluster = $CLUSTERSIZE >> terraform.tfvars
fi
for RETRY in 1 2 3; do
if terraform apply -auto-approve; then
touch terraform.ok
@@ -324,8 +326,8 @@ _cmd_clusterize() {
grep KUBECOLOR_ /etc/ssh/sshd_config || echo 'AcceptEnv KUBECOLOR_*' | sudo tee -a /etc/ssh/sshd_config
sudo systemctl restart ssh.service"
pssh -I < tags/$TAG/clusters.txt "
grep -w \$PSSH_HOST | tr ' ' '\n' > /tmp/cluster"
pssh -I < tags/$TAG/clusters.tsv "
grep -w \$PSSH_HOST | tr '\t' '\n' > /tmp/cluster"
pssh "
echo \$PSSH_HOST > /tmp/ipv4
head -n 1 /tmp/cluster | sudo tee /etc/ipv4_of_first_node
@@ -346,6 +348,10 @@ _cmd_clusterize() {
done < /tmp/cluster
"
while read line; do
printf "%s\tssh -l %s\t%s\n" "$USER_PASSWORD" "$USER_LOGIN" "$line"
done < tags/$TAG/clusters.tsv > tags/$TAG/login.tsv
echo cluster_ok > tags/$TAG/status
}
@@ -934,6 +940,14 @@ _cmd_inventory() {
FIXME
}
_cmd login "Show login information for a group of instances"
_cmd_login() {
TAG=$1
need_tag $TAG
cat tags/$TAG/login.tsv
}
_cmd maketag "Generate a quasi-unique tag for a group of instances"
_cmd_maketag() {
if [ -z $USER ]; then
@@ -984,6 +998,8 @@ _cmd_stage2() {
cd tags/$TAG/stage2
terraform init -upgrade
terraform apply -auto-approve
terraform output -raw login_tsv > ../login.tsv
terraform output -raw ips_txt > ../ips.txt
}
_cmd standardize "Deal with non-standard Ubuntu cloud images"
@@ -1163,8 +1179,8 @@ _cmd_tags() {
cd tags
echo "[#] [Status] [Tag] [Mode] [Provider]"
for tag in *; do
if [ -f $tag/ips.txt ]; then
count="$(wc -l < $tag/ips.txt)"
if [ -f $tag/login.tsv ]; then
count="$(wc -l < $tag/login.tsv)"
else
count="?"
fi

View File

@@ -6,27 +6,26 @@ import jinja2
# Read settings from user-provided settings file
context = yaml.safe_load(open(sys.argv[1]))
context = dict(
cards_template = "mlops.html",
paper_size = "Letter",
) # {} # yaml.safe_load(open(sys.argv[1]))
ips = list(open("ips.txt"))
clustersize = context["clustersize"]
logins = list(open("login.tsv"))
context["logins"] = []
for login in logins:
password, command, ipaddr, ipaddrs = login.split("\t", 3)
context["logins"].append(dict(
password=password,
command=command,
ipaddr=ipaddr,
ipaddrs=ipaddrs,
))
print("---------------------------------------------")
print(" Number of IPs: {}".format(len(ips)))
print(" VMs per cluster: {}".format(clustersize))
print(" Number of cards: {}".format(len(logins)))
print("---------------------------------------------")
assert len(ips)%clustersize == 0
clusters = []
while ips:
cluster = ips[:clustersize]
ips = ips[clustersize:]
clusters.append(cluster)
context["clusters"] = clusters
template_file_name = context["cards_template"]
template_file_path = os.path.join(
os.path.dirname(__file__),
@@ -35,23 +34,23 @@ template_file_path = os.path.join(
template_file_name
)
template = jinja2.Template(open(template_file_path).read())
with open("ips.html", "w") as f:
f.write(template.render(**context))
print("Generated ips.html")
with open("cards.html", "w") as f:
f.write(template.render(**context))
print("Generated cards.html")
try:
import pdfkit
paper_size = context["paper_size"]
margin = {"A4": "0.5cm", "Letter": "0.2in"}[paper_size]
with open("ips.html") as f:
pdfkit.from_file(f, "ips.pdf", options={
with open("cards.html") as f:
pdfkit.from_file(f, "cards.pdf", options={
"page-size": paper_size,
"margin-top": margin,
"margin-bottom": margin,
"margin-left": margin,
"margin-right": margin,
})
print("Generated ips.pdf")
print("Generated cards.pdf")
except ImportError:
print("WARNING: could not import pdfkit; did not generate ips.pdf")
print("WARNING: could not import pdfkit; did not generate cards.pdf")

View File

@@ -217,7 +217,18 @@ resource "kubernetes_certificate_signing_request_v1" "cluster_admin_${index}" {
%{ endfor ~}
output "ip_addresses_of_nodes" {
output "ips_txt" {
value = join("\n", [
%{ for index, cluster in clusters ~}
join("\n", concat(
split(" ", file("./externalips.${index}"))
)),
%{ endfor ~}
""
])
}
output "login_tsv" {
value = join("\n", [
%{ for index, cluster in clusters ~}
join("\t", concat(
@@ -228,5 +239,6 @@ output "ip_addresses_of_nodes" {
split(" ", file("./externalips.${index}"))
)),
%{ endfor ~}
""
])
}

View File

@@ -71,10 +71,10 @@ resource "local_file" "ip_addresses" {
resource "local_file" "clusters" {
content = join("", formatlist("%s\n", [
for cid in range(1, 1 + var.how_many_clusters) :
join(" ",
join("\t",
[for nid in range(1, 1 + var.nodes_per_cluster) :
local.ip_addresses[format("c%03dn%03d", cid, nid)]
])]))
filename = "clusters.txt"
filename = "clusters.tsv"
file_permission = "0600"
}