Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly use runcmd and scripts-user in cloud-init?

By looking at runcmd code (/usr/lib/python2.6/site-packages/cloudinit/config/cc_runcmd.py) I noticed there's no 'frequency' specified in comparison to other. Beside the only thing that scripts does is to save the scripts given as shells script under /var/lib/cloud/instance/scripts/runcmd .

So if I verbosely specify the modules, I MUST set the frequency.

cloud_config_modules:
 - mounts
 - locale
 - set-passwords
 - timezone
 - [ runcmd, always ]

cloud_final_modules:
 - scripts-per-once
 - scripts-per-boot
 - scripts-per-instance
 - [ scripts-user, always ]
 - ssh-authkey-fingerprints

and the scripts created by this particular can be run by 'scripts-user', so I need to specify i.e.

- [ scripts-user, always ]

to make runcmd scripts working

Is it proper way to do so ? The documentation (at least current 0.7.7) lacks proper explanation of runcmd and user-scripts and how they can be utilized

I also don't understand difference between all the mode, once (first time the instance boot), instance(???), always ( that I understand), ????boot(that one exist? it doesn't seem to work...

EDIT:

ok, i have found in cloudinit/settings.py:

# Valid frequencies of handlers/modules
PER_INSTANCE = "once-per-instance"
PER_ALWAYS = "always"
PER_ONCE = "once"

and beside I found an explanation in this script:

[root@euca-10-254-97-216 ~]# cloud-init-per -h
Usage: cloud-init-per frequency name cmd [ arg1 [ arg2 [ ... ] ]
   run cmd with arguments provided.

   This utility can make it easier to use boothooks or bootcmd
   on a per "once" or "always" basis.

   If frequency is:
      * once: run only once (do not re-run for new instance-id)
      * instance: run only the first boot for a given instance-id
      * always: run every boot

But I don't catch the difference between 'once' and 'instance'. So if the same image (rather snapshot) with new instance id in the case 'once' it won't start ? in case 'instance' it will ?

like image 907
mastier Avatar asked Nov 12 '15 07:11

mastier


People also ask

Where are cloud-init scripts stored?

Cloud-init config is provided in two places: /etc/cloud/cloud. cfg. /etc/cloud/cloud.

What is cloud-init script?

cloud-init is a software package that automates the initialization of cloud instances during system boot. You can configure cloud-init to perform a variety of tasks. Some sample tasks that cloud-init can perform include: Configuring a host name. Installing packages on an instance.

Where does cloud-init look for user data?

So cloud-init will retrieve your user-data from an attached ISO image (from a file named user-data. txt ). When cloud-init runs, it will typically populate /var/lib/cloud-init/instance with any information retrieved from the cloud provider, so you should find a copy of the user data in that directory.

Does user data run on reboot EC2?

By default, user data scripts and cloud-init directives run only during the first boot cycle when an EC2 instance is launched.


1 Answers

I think the difference matters if you are creating an image that will be used to launch more instances.

Imagine you run the cloud-init scripts to set an instance. Later you make an image of that instance from which you can launch more instances.

When you start a new instance from the image:

  • the scripts with frequency "once" will not run again
  • the scripts with frequency "instance" will run on the first boot, and never again in that instance's lifetime
  • the scripts with frequency "always" will run during every boot
like image 185
Iain Samuel McLean Elder Avatar answered Nov 14 '22 23:11

Iain Samuel McLean Elder