Commit b5e020f6 authored by Joe Nash's avatar Joe Nash Committed by Chris Molozian
Browse files

DO app via packer

parent 56289717
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
# Build Automation with Packer

[Packer](https://www.packer.io/intro/index.html) is a tool for creating images from a single source configuration. Using this Packer template reduces the entire process of creating, configuring, validating, and snapshotting a build Droplet to a single command:

```
packer build marketplace-image.json
```

This Packer template is configured to create a snapshot with Nakama and CockroachDB.
## Usage

To get started, you'll need to [install Packer](https://www.packer.io/intro/getting-started/install.html) and [create a DigitalOcean personal access token](https://www.digitalocean.com/docs/api/create-personal-access-token/) and set it to the `DIGITALOCEAN_API_TOKEN` environment variable. Running `packer build marketplace-image.json` without any other modifications will create a build Droplet configured with Nakama & CockroachDB, clean and verify it, then power it down and snapshot it.

> ⚠️ The image validation script in `scripts/99-img_check.sh` is copied from the [top-level `scripts` directory](../scripts) in the [DigitalOcean Marketplace Partnners repository](https://github.com/digitalocean/marketplace-partners). The top-level location is the script's canonical source, so make sure you're using the latest version from there.

This Packer template uses binaries for both Nakama and CockroachDB. The URL for each binary is set via variables at the top of the template.

## Configuration Details

By using [Packer's DigitalOcean Builder](https://www.packer.io/docs/builders/digitalocean.html) to integrate with the [DigitalOcean API](https://developers.digitalocean.com/), this template fully automates Marketplace image creation.

This template uses Packer's [file provisioner](https://www.packer.io/docs/provisioners/file.html) to upload complete directories to the Droplet. The contents of `files/var/` will be uploaded to `/var/`. Likewise, the contents of `files/etc/` will be uploaded to `/etc/`. One important thing to note about the file provisioner, from Packer's docs:

> The destination directory must already exist. If you need to create it, use a shell provisioner just prior to the file provisioner in order to create the directory. If the destination directory does not exist, the file provisioner may succeed, but it will have undefined results.

This template also uses Packer's [shell provisioner](https://www.packer.io/docs/provisioners/shell.html) to run scripts from the `/scripts` directory and install APT packages using an inline task.

Learn more about using Packer in [the official Packer documentation](https://www.packer.io/docs/index.html).
+9 −0
Original line number Diff line number Diff line
[Unit]
Description=Nakama server

[Service]
ExecStart=/usr/local/bin/nakama --config /root/nakama/nakama/config.yml
KillMode=process

[Install]
WantedBy=multi-user.target
 No newline at end of file
+40 −0
Original line number Diff line number Diff line
#!/bin/sh
#
# Configured as part of the DigitalOcean 1-Click Image build process

myip=$(hostname -I | awk '{print$1}')
cat << "EOF"
********************************************************************************
                                                                                                                                                                                                  
  ,,                                                                                                
  ,,                                                                                                
  ,`       `,,,,,.                                                                                  
  ,`     ,,,,,,,,,,,`                                                                               
  ,`   .,,,,,,,,,,,,,,                                                                              
  ,`  ,,,,,,,,,   `,,,,                                                                             
  ,` ,,,,:,:;,,,,,,,,,,,                                                                            
  ,``,,,::';;,,,,,,, `,,,                                                                           
  ,`,,,::::';,,,,,,,, .,,`         +++     `++           ++                                         
  ,,,,:::::';:,,,,,,,, ,,,         +++;    `++           ++                                         
  ,,,,:::::';;,,,,,,,,` ,,         ++++    `++           ++                                         
  ,,,:::::::::::,,,,,,, ,,,        +++++   `++    ;';    ++   ,::   .'',    ,. :':   ;':     `;':   
  ,,,::::::::::::,,,,,, ,,,        ++,+++  `++  ++++++;  ++  .++   ++++++   +++++++:+++++   ++++++` 
  ,,,:::::::::::::,,,,,,,,,        ++, ++` `++  ..   ++  ++ `++    +   ;++  ++'  +++:  ++'  +`  .++ 
  .,,::::::::  :::,,,,,,,,,        ++, ;++ `++       ++  ++ ++`         ++  ++   `++   .++       ++ 
  .,,::::::::  :::::::'':,,        ++,  +++`++   ++++++  +++++     `++++++  ++    ++   .++   ++++++ 
  `,,:::::::::::::::::'',,,        ++,   ++;++  +++;:++  ++ ++,   `+++::++  ++    ++   .++  +++::++ 
   ,,:::::::::::::::::;',,.        ++,   .++++ `++   ++  ++ `++   ++`   ++  ++    ++   .++ '+:   ++ 
   ,,,::::::::::::::::::,,         ++,    ++++ `++  :++  ++  ;++  ++:  +++  ++    ++   .++ '+'  '++ 
   `,,:::::::::::::::::,,,         ++,     +++  +++++++  ++   +++ `+++++++  ++    ++   .++  +++++++ 
    ,,,:::::::::::::::,,,          ,,`      ..   ;++``;  ,,    ,,.  ++' ;;  ::    ::   `::   '+' ;; 
     ,,,::::::::::::::,,,                                                                           
     .,,,:::::::::::,,,,                                                                            
      .,,,,::::::::,,,,                                                                             
        ,,,,,,,,,,,,,,                                                                              
         .,,,,,,,,,,                                                                                
            `,,,.                                                                                   
                                                                                                    
********************************************************************************

To delete this message of the day: rm -rf $(readlink -f ${0})
EOF
 No newline at end of file
+14 −0
Original line number Diff line number Diff line
#!/bin/bash

# Protect the droplet
ufw limit ssh
ufw allow https
ufw allow http
ufw --force enable

# Start Cockroach
cockroach start --background --insecure --store=path='/root/cdb-store1/' --listen-addr=localhost
nakama migrate up
systemctl enable nakama
systemctl start nakama
+95 −0
Original line number Diff line number Diff line
{
  "variables": {
    "token": "{{env `DIGITALOCEAN_API_TOKEN`}}",
    "image_name": "marketplace-snapshot-{{timestamp}}",
    "nakama_url": "https://github.com/heroiclabs/nakama/releases/download/v2.5.1/nakama-2.5.1-linux-amd64.tar.gz",
    "cockroach_url": "https://binaries.cockroachdb.com/cockroach-v2.1.6.linux-amd64.tgz"
  },
  "builders": [
    {
      "type": "digitalocean",
      "api_token": "{{user `token`}}",
      "image": "ubuntu-18-04-x64",
      "region": "nyc3",
      "size": "s-1vcpu-1gb",
      "ssh_username": "root",
      "snapshot_name": "{{user `image_name`}}"
    }
  ],
  "provisioners": [
    { 
      "type": "shell",
      "inline": [ 
        "sleep 30"
      ]
    },
    {
      "type": "file",
      "source": "files/etc/",
      "destination": "/etc"
    },
    {
      "type": "file",
      "source": "files/var/",
      "destination": "/var"
    },
    {
      "type": "shell",
      "inline": [
        "echo 'setting file permissions'",
        "chmod 644 /etc/systemd/system/nakama.service",
        "chmod +x /etc/update-motd.d/99-one-click",
        "chmod +x /var/lib/cloud/scripts/per-instance/001_onboot"
      ]
    },
    {
      "type": "shell",
      "inline": [
        "apt-get -qqy update",
        "apt-get -qqy -o Dpkg::Options::='--force-confdef' -o Dpkg::Options::='--force-confold' upgrade"
      ]
    },
    {
      "type": "shell",
      "environment_vars": [
        "nakamaurl={{user `nakama_url`}}",
        "cockroachurl={{user `cockroach_url`}}"
      ],
      "inline": [
        "wget -qO- $nakamaurl | tar  xvz ",
        "wget -qO- $cockroachurl | tar  xvz --strip-components=1",
        "mv -i cockroach /usr/local/bin",
        "mv -i nakama /usr/local/bin",
        "systemctl enable nakama"
      ]
    },
    {
      "type": "shell",
      "inline": [
        "cockroach start --background --insecure --store=path='/root/cdb-store1/' --listen-addr=localhost",
        "nakama migrate up",
        "cockroach quit --insecure"
      ]
    },
    {
      "type": "shell",
      "inline": [
        "mkdir -p ~/nakama",
        "mkdir -p ~/nakama/data"
      ]
    },
    { 
      "type": "file",
      "source": "files/nakama/",
      "destination": "~/nakama/"
    },
    {
      "type": "shell",
      "scripts": [
        "scripts/01-test",
        "scripts/90-cleanup.sh",
        "scripts/99-img_check.sh"
      ]
    }
  ]
}
Loading