MaaS:使用Cloud-Init自动定制部署流程

MaaS:使用Cloud-Init自动定制部署流程

我尝试了一周,但都没有成功!我想在机器部署过程中创建一个 MACVLAN。我已经使用 Cloud-Init 语法为此过程提供了代码:

#cloud-config

write_files:
-   content: |
        #!/bin/bash

        sudo ip link add eth0 link enp0s3 type macvlan mode bridge
        sudo ifconfig eth0 up
    path: /tmp/macvlan.sh
    permissions: '777'
-   content: |
        [Unit]
        Description=macvlan setup service

        [Service]
        ExecStart=/etc/init.d/macvlan.sh

        [Install]
        WantedBy=multi-user.target
    path: /tmp/macvlan.service
    permissions: '777'

runcmd:
-   sudo mv /tmp/macvlan.sh /etc/init.d/macvlan.sh
-   sudo mv /tmp/macvlan.service /etc/systemd/system/macvlan.service
-   systemctl enable macvlan
-   systemctl start macvlan

所以现在我想知道如何使用这个代码自动地期间部署过程我知道有一种方法可以手动使用以下代码:

maas $PROFILE machine deploy $SYSTEM_ID user_data=$(base64 ./import_key.sh)

但我希望 MaaS 自动为所有机器执行此操作(因为我使用 Juju 控制器部署新机器,无法手动执行此操作)。如果无法使用 Cloud-Init,那么是否可以使用 Curtin?如何操作?

答案1

我终于找到了答案。我在/etc/maas/preseeds/curtin_userdata文件中的以下late_commands:行后添加了以下代码...

  foo_00: ["sh", "-c", "echo === Start Customization Scripts ==="]
  foo_02: ["curtin", "in-target", "--", "sh", "-c", "sudo touch /etc/init.d/macvlan.sh"]
  foo_03: ["curtin", "in-target", "--", "sh", "-c", "sudo touch /etc/systemd/system/macvlan.service"]
  foo_04: ["curtin", "in-target", "--", "sh", "-c", "sudo chmod 777 /etc/init.d/macvlan.sh"]
  foo_05: ["curtin", "in-target", "--", "sh", "-c", "sudo chmod 777 /etc/systemd/system/macvlan.service"]
  foo_06: ["curtin", "in-target", "--", "sh", "-c", "sudo echo '#!/bin/bash' >> /etc/init.d/macvlan.sh"]
  foo_07: ["curtin", "in-target", "--", "sh", "-c", "sudo echo ' ' >> /etc/init.d/macvlan.sh"]
  foo_08: ["curtin", "in-target", "--", "sh", "-c", "sudo echo 'sudo ip link add eth0 link eno1 type macvlan mode bridge' >> /etc/init.d/macvlan.sh"]
  foo_09: ["curtin", "in-target", "--", "sh", "-c", "sudo echo 'sudo ifconfig eth0 up' >> /etc/init.d/macvlan.sh"]
  foo_10: ["curtin", "in-target", "--", "sh", "-c", "sudo echo '[Unit]' >> /etc/systemd/system/macvlan.service"]
  foo_11: ["curtin", "in-target", "--", "sh", "-c", "sudo echo 'Description=macvlan setup service' >> /etc/systemd/system/macvlan.service"]
  foo_12: ["curtin", "in-target", "--", "sh", "-c", "sudo echo ' ' >> /etc/systemd/system/macvlan.service"]
  foo_13: ["curtin", "in-target", "--", "sh", "-c", "sudo echo '[Service]' >> /etc/systemd/system/macvlan.service"]
  foo_14: ["curtin", "in-target", "--", "sh", "-c", "sudo echo 'ExecStart=/etc/init.d/macvlan.sh' >> /etc/systemd/system/macvlan.service"]
  foo_15: ["curtin", "in-target", "--", "sh", "-c", "sudo echo ' ' >> /etc/systemd/system/macvlan.service"]
  foo_16: ["curtin", "in-target", "--", "sh", "-c", "sudo echo '[Install]' >> /etc/systemd/system/macvlan.service"]
  foo_17: ["curtin", "in-target", "--", "sh", "-c", "sudo echo 'WantedBy=multi-user.target' >> /etc/systemd/system/macvlan.service"]
  foo_18: ["curtin", "in-target", "--", "sh", "-c", "sudo systemctl enable macvlan"]
  foo_19: ["curtin", "in-target", "--", "sh", "-c", "sudo systemctl start macvlan"]
  foo_20: ["sh", "-c", "echo === Done Customization Scripts ==="]

谢谢大家。

相关内容