#cloud-config
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

packages:
- default-jre
- netcat

write_files:
- path: /run/zookeeper.service
  permissions: 0644
  content: |
    [Unit]
    Description=ZooKeeper Service
    Documentation=http://zookeeper.apache.org
    Requires=network.target
    After=network.target
    [Service]
    Type=forking
    User=zookeeper
    Group=zookeeper
    ExecStart=/usr/local/zookeeper/bin/zkServer.sh start /usr/local/zookeeper/conf/zoo.cfg
    ExecStop=/usr/lib/zookeeper/bin/zkServer.sh stop /usr/local/zookeeper/conf/zoo.cfg
    ExecReload=/usr/lib/zookeeper/bin/zkServer.sh restart /usr/local/zookeeper/conf/zoo.cfg
    WorkingDirectory=/usr/local/zookeeper
    PIDFile=/usr/local/zookeeper/data/zookeeper_server.pid
    Environment=ZK_SERVER_HEAP=${zk_heap}
    [Install]
    WantedBy=default.target
    
- path: /run/zoo.cfg
  permissions: 0644
  content: |
    # The number of milliseconds of each tick
    tickTime=2000
    # The number of ticks that the initial 
    # synchronization phase can take
    initLimit=10
    # The number of ticks that can pass between 
    # sending a request and getting an acknowledgement
    syncLimit=5
    # the directory where the snapshot is stored.
    # do not use /tmp for storage, /tmp here is just 
    # example sakes.
    dataDir=/usr/local/zookeeper/data
    # the port at which the clients will connect
    clientPort=2181
    # the maximum number of client connections.
    # increase this if you need to handle more clients
    #maxClientCnxns=60
    #
    # Be sure to read the maintenance section of the 
    # administrator guide before turning on autopurge.
    #
    # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
    #
    # The number of snapshots to retain in dataDir
    #autopurge.snapRetainCount=3
    # Purge task interval in hours
    # Set to "0" to disable auto purge feature
    #autopurge.purgeInterval=1

    ## Metrics Providers
    #
    # https://prometheus.io Metrics Exporter
    #metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
    #metricsProvider.httpPort=7000
    #metricsProvider.exportJvmInfo=true
    4lw.commands.whitelist=stat, ruok, conf, isro
    %{~ for node in nodes ~}
    server.${node}=zk-${node}.${domain}:2888:3888
    %{ endfor ~}
    
- path: /run/install_zk.sh
  permissions: 0700
  content: |
    #!/bin/bash
    echo create zk user
    useradd zookeeper -s /sbin/nologin || :

    echo Installing Zookeeper v${version}
    wget -O /run/zk.tgz https://mirrors.huaweicloud.com/apache/zookeeper/zookeeper-${version}/apache-zookeeper-${version}-bin.tar.gz
    mkdir -p /usr/local/zookeeper
    tar zxvf /run/zk.tgz --strip 1 -C /usr/local/zookeeper
    mkdir -p /usr/local/zookeeper/data
    echo ${index} > /usr/local/zookeeper/data/myid
    /bin/cp -avf /run/zoo.cfg /usr/local/zookeeper/conf/zoo.cfg
    /bin/cp -avf /run/zookeeper.service /usr/lib/systemd/system/zookeeper.service
    chown -R zookeeper:zookeeper /usr/local/zookeeper
    systemctl daemon-reload
    systemctl enable zookeeper
    systemctl start zookeeper

runcmd:
- bash /run/install_zk.sh
  
