project-lakitu: add ntp module in cloud-init

we use ubuntu base of cloud-init on COS, where as in ubuntu,
the chrony service runs as chrony.service but on COS it runs
as chronyd service. Ubuntu uses the default configuration at /etc/chrony.conf
but on COS, the default conf is present at /etc/chrony/chrony.conf

21.2-enable-ntp-for-cos.patch does the following
* replace chrony service with chronyd.service as per COS
* replace default conf path to be /etc/chrony/chrony.conf
* clean up the template used to generate /etc/chrony/chrony.conf
when ntp server is configured in cloud-init. This is to make conf
similar to that of COS.



BUG=b/197692631
TEST=manual validation
RELEASE_NOTE=Enable configuring NTP server using cloud-init

Change-Id: I9e1d0719cfb0ee1e0a21097340bc063679b0c559
Reviewed-on: https://cos-review.googlesource.com/c/cos/overlays/board-overlays/+/22010
Reviewed-by: Vaibhav Rustagi <vaibhavrustagi@google.com>
Tested-by: Cusky Presubmit Bot <presubmit@cos-infra-prod.iam.gserviceaccount.com>
diff --git a/project-lakitu/app-emulation/cloud-init-config/cloud-init-config-1.0.0-r5.ebuild b/project-lakitu/app-emulation/cloud-init-config/cloud-init-config-1.0.0-r6.ebuild
similarity index 100%
rename from project-lakitu/app-emulation/cloud-init-config/cloud-init-config-1.0.0-r5.ebuild
rename to project-lakitu/app-emulation/cloud-init-config/cloud-init-config-1.0.0-r6.ebuild
diff --git a/project-lakitu/app-emulation/cloud-init-config/cloud-init-config-1.0.0.ebuild b/project-lakitu/app-emulation/cloud-init-config/cloud-init-config-1.0.0.ebuild
index 80dc656..54f33a2 100644
--- a/project-lakitu/app-emulation/cloud-init-config/cloud-init-config-1.0.0.ebuild
+++ b/project-lakitu/app-emulation/cloud-init-config/cloud-init-config-1.0.0.ebuild
@@ -26,8 +26,6 @@
 	insinto /etc/cloud
 	if use platform_azure; then
 		doins "${FILESDIR}"/azure/cloud.cfg
-	elif use platform_vsphere; then
-		doins "${FILESDIR}"/vsphere/cloud.cfg
 	else
 		doins "${FILESDIR}"/cloud.cfg
 	fi
diff --git a/project-lakitu/app-emulation/cloud-init-config/files/cloud.cfg b/project-lakitu/app-emulation/cloud-init-config/files/cloud.cfg
index df1649e..3d023ae 100644
--- a/project-lakitu/app-emulation/cloud-init-config/files/cloud.cfg
+++ b/project-lakitu/app-emulation/cloud-init-config/files/cloud.cfg
@@ -37,11 +37,13 @@
 
 # The modules that run in the 'config' stage
 cloud_config_modules:
+ - [ntp, once]
  - [mounts, once]
  - [ssh-import-id, once]
  - [timezone, once]
  - [disable-ec2-metadata, always]
  - [runcmd, always]
+ - [set-passwords, once]
 
 # The modules that run in the 'final' stage
 cloud_final_modules:
diff --git a/project-lakitu/app-emulation/cloud-init-config/files/vsphere/cloud.cfg b/project-lakitu/app-emulation/cloud-init-config/files/vsphere/cloud.cfg
deleted file mode 100644
index 742f1f6..0000000
--- a/project-lakitu/app-emulation/cloud-init-config/files/vsphere/cloud.cfg
+++ /dev/null
@@ -1,65 +0,0 @@
-# The top level settings are used as module
-# and system configuration.
-
-# If this is set, 'root' will not be able to ssh in and they
-# will get a message to login instead as the above $user (ubuntu)
-disable_root: true
-
-# This will cause the set+update hostname module to not operate (if true)
-preserve_hostname: true
-
-# Example datasource config
-# datasource:
-#    Ec2:
-#      metadata_urls: [ 'blah.com' ]
-#      timeout: 5 # (defaults to 50 seconds)
-#      max_wait: 10 # (defaults to 120 seconds)
-
-# Disable network-config because Lakitu doesn't support any network renders
-# of cloud-init (eni, netplan, sysconfig).
-network:
-   config: disabled
-
-# Lakitu specific: Lakitu has a "stateless" cloud-init, so the following modules
-# are all executed upon reboot. Therefore, the concept of module run frequency
-# in lakitu is different from Ubuntu. There are only two run frequencies "always"
-# and "once" in lakitu. If we rerun cloud-init without rebooting the instance,
-# modules with "always" frequency will be rerun, while modules with "once"
-# frequency will not.
-
-# The modules that run in the 'init' stage
-cloud_init_modules:
- - [bootcmd, once]
- - [update_etc_hosts, always]
- - [users-groups, once]
- - [write-files, always]
- - [ssh, once]
-
-# The modules that run in the 'config' stage
-cloud_config_modules:
- - [mounts, once]
- - [ssh-import-id, once]
- - [timezone, once]
- - [disable-ec2-metadata, always]
- - [runcmd, always]
- - [set-passwords, once]
-
-# The modules that run in the 'final' stage
-cloud_final_modules:
- - [rightscale_userdata, once]
- - [scripts-vendor, once]
- - [scripts-per-once, once]
- - [scripts-per-boot, once]
- - [scripts-per-instance, once]
- - [scripts-user, always]
- - [ssh-authkey-fingerprints, once]
- - [keys-to-console, once]
- - [phone-home, once]
- - [final-message, always]
-
-# System and/or distro specific settings
-# (not accessible to handlers/transforms)
-system_info:
-   paths:
-      cloud_dir: /var/lib/cloud/
-      templates_dir: /etc/cloud/templates/
diff --git a/project-lakitu/app-emulation/cloud-init/cloud-init-21.2-r1.ebuild b/project-lakitu/app-emulation/cloud-init/cloud-init-21.2-r2.ebuild
similarity index 100%
rename from project-lakitu/app-emulation/cloud-init/cloud-init-21.2-r1.ebuild
rename to project-lakitu/app-emulation/cloud-init/cloud-init-21.2-r2.ebuild
diff --git a/project-lakitu/app-emulation/cloud-init/cloud-init-21.2.ebuild b/project-lakitu/app-emulation/cloud-init/cloud-init-21.2.ebuild
index c1c6b20..f83b4bc 100644
--- a/project-lakitu/app-emulation/cloud-init/cloud-init-21.2.ebuild
+++ b/project-lakitu/app-emulation/cloud-init/cloud-init-21.2.ebuild
@@ -65,13 +65,14 @@
 	"${FILESDIR}/${PV}-datasource-gce-platform-google.patch"
 	"${FILESDIR}/${PV}-add-retries-gce-metadata-server.patch"
 
-	# Ensures the user added through cloud-init have UID in range [2000, 5000) 
+	# Ensures the user added through cloud-init have UID in range [2000, 5000)
 	"${FILESDIR}/${PV}-stable-uid.patch"
-        
 	"${FILESDIR}/${PV}-fix-cross-compile.patch"
-        
 	# Ensure we don't add ssh keys to the root user
 	"${FILESDIR}/${PV}-skip-root-ssh-keys.patch"
+
+	# Enable NTP for COS in cloud-init
+	"${FILESDIR}/21.2-enable-ntp-for-cos.patch"
 )
 
 python_prepare_all() {
@@ -81,8 +82,8 @@
 	else
 		PATCHES+=("${FILESDIR}/${PV}-remove-sshd-dependency.patch")
 	fi
-	
-        # Remove dhclient dependency on cloud-init
+
+	# Remove dhclient dependency on cloud-init
 	if use platform_azure; then
 		PATCHES+=("${FILESDIR}"/cloud-init-azure-remove-dhclient.patch)
 	fi
diff --git a/project-lakitu/app-emulation/cloud-init/files/21.2-enable-ntp-for-cos.patch b/project-lakitu/app-emulation/cloud-init/files/21.2-enable-ntp-for-cos.patch
new file mode 100644
index 0000000..8eec988
--- /dev/null
+++ b/project-lakitu/app-emulation/cloud-init/files/21.2-enable-ntp-for-cos.patch
@@ -0,0 +1,73 @@
+From 36029e9ec08d85a57f834f617c9ee5a73aa80090 Mon Sep 17 00:00:00 2001
+From: Varsha Teratipally <teratipally@google.com>
+Date: Wed, 25 Aug 2021 22:03:11 +0000
+Subject: [PATCH]  enable NTP for cos
+
+---
+ cloudinit/config/cc_ntp.py        |  8 ++------
+ templates/chrony.conf.ubuntu.tmpl | 15 +--------------
+ 2 files changed, 3 insertions(+), 20 deletions(-)
+
+diff --git a/cloudinit/config/cc_ntp.py b/cloudinit/config/cc_ntp.py
+index 5498bbaa..fcba31cd 100644
+--- a/cloudinit/config/cc_ntp.py
++++ b/cloudinit/config/cc_ntp.py
+@@ -28,9 +28,9 @@ distros = ['centos', 'debian', 'fedora', 'opensuse', 'rhel', 'sles', 'ubuntu']
+ NTP_CLIENT_CONFIG = {
+     'chrony': {
+         'check_exe': 'chronyd',
+-        'confpath': '/etc/chrony.conf',
++        'confpath': '/etc/chrony/chrony.conf',
+         'packages': ['chrony'],
+-        'service_name': 'chrony',
++        'service_name': 'chronyd',
+         'template_name': 'chrony.conf.{distro}',
+         'template': None,
+     },
+@@ -450,10 +450,6 @@ def write_ntp_config_template(distro_name, service_name=None, servers=None,
+         servers = generate_server_names(distro_name)
+         LOG.debug(
+             'Adding distro default ntp servers: %s', ','.join(servers))
+-    elif len(servers) == 0 and len(pools) == 0:
+-        pools = generate_server_names(distro_name)
+-        LOG.debug(
+-            'Adding distro default ntp pool servers: %s', ','.join(pools))
+
+     if not path:
+         raise ValueError('Invalid value for path parameter')
+diff --git a/templates/chrony.conf.ubuntu.tmpl b/templates/chrony.conf.ubuntu.tmpl
+index 50a6f518..3b538f95 100644
+--- a/templates/chrony.conf.ubuntu.tmpl
++++ b/templates/chrony.conf.ubuntu.tmpl
+@@ -13,26 +13,13 @@ pool {{pool}} iburst
+ {%- if servers %}# servers
+ {% endif %}
+ {% for server in servers -%}
+-server {{server}} iburst
++server {{server}} iburst minpoll 4 maxpoll 4
+ {% endfor %}
+
+-# This directive specify the location of the file containing ID/key pairs for
+-# NTP authentication.
+-keyfile /etc/chrony/chrony.keys
+-
+ # This directive specify the file into which chronyd will store the rate
+ # information.
+ driftfile /var/lib/chrony/chrony.drift
+
+-# Uncomment the following line to turn logging on.
+-#log tracking measurements statistics
+-
+-# Log files location.
+-logdir /var/log/chrony
+-
+-# Stop bad estimates upsetting machine clock.
+-maxupdateskew 100.0
+-
+ # This directive enables kernel synchronisation (every 11 minutes) of the
+ # real-time clock. Note that it can’t be used along with the 'rtcfile' directive.
+ rtcsync
+--
+2.33.0.259.gc128427fd7-goog
+
+