From 50f996346d7d1553f9ff9a4b8042a5c4af9cd00d Mon Sep 17 00:00:00 2001 From: Scott Lowe Date: Sun, 21 Apr 2019 14:13:26 -0600 Subject: [PATCH] Create a Pulumi learning environment Add files for a Vagrant-backed Pulumi learning environment. Add files for using Pulumi to spin up an EC2 instance. Environment is not currently functional. Signed-off-by: Scott Lowe --- pulumi/README.md | 0 pulumi/Vagrantfile | 82 +++++++++++++++++++++++++++++++ pulumi/ansible.cfg | 6 +++ pulumi/js-simple-ec2/Pulumi.yaml | 8 +++ pulumi/js-simple-ec2/index.js | 34 +++++++++++++ pulumi/js-simple-ec2/package.json | 9 ++++ pulumi/js-simple-ec2/playbook.yml | 22 +++++++++ pulumi/machines.yml | 9 ++++ pulumi/provision.yml | 59 ++++++++++++++++++++++ 9 files changed, 229 insertions(+) create mode 100644 pulumi/README.md create mode 100644 pulumi/Vagrantfile create mode 100644 pulumi/ansible.cfg create mode 100644 pulumi/js-simple-ec2/Pulumi.yaml create mode 100644 pulumi/js-simple-ec2/index.js create mode 100644 pulumi/js-simple-ec2/package.json create mode 100644 pulumi/js-simple-ec2/playbook.yml create mode 100644 pulumi/machines.yml create mode 100644 pulumi/provision.yml diff --git a/pulumi/README.md b/pulumi/README.md new file mode 100644 index 0000000..e69de29 diff --git a/pulumi/Vagrantfile b/pulumi/Vagrantfile new file mode 100644 index 0000000..67270fc --- /dev/null +++ b/pulumi/Vagrantfile @@ -0,0 +1,82 @@ +# -*- mode: ruby -*- +# vi: set ft=ruby : + +# Specify minimum Vagrant version and Vagrant API version +Vagrant.require_version '>= 1.6.0' +VAGRANTFILE_API_VERSION = '2' + +# Require 'yaml' module +require 'yaml' + +# Read YAML file with VM details (box, CPU, RAM, IP addresses) +# Edit machines.yml to change VM configuration details +machines = YAML.load_file(File.join(File.dirname(__FILE__), 'machines.yml')) + +# Create and configure the VMs +Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| + + # Always use Vagrant's default insecure key + config.ssh.insert_key = false + + # Iterate through entries in YAML file to create VMs + machines.each do |machine| + + # Configure the VMs per details in machines.yml + config.vm.define machine['name'] do |srv| + + # Don't check for box updates + srv.vm.box_check_update = false + + # Specify the hostname of the VM + srv.vm.hostname = machine['name'] + + # Specify the Vagrant box to use (use VMware box by default) + srv.vm.box = machine['box']['vmw'] + + # Configure default synced folder (disable by default) + if machine['sync_disabled'] != nil + srv.vm.synced_folder '.', '/vagrant', disabled: machine['sync_disabled'] + else + srv.vm.synced_folder '.', '/vagrant', disabled: true + end #if machine['sync_disabled'] + + # Assign additional private network + if machine['ip_addr'] != nil + srv.vm.network 'private_network', ip: machine['ip_addr'] + end # if machine['ip_addr'] + + # Configure CPU & RAM per settings in machines.yml (Fusion) + srv.vm.provider 'vmware_fusion' do |vmw| + vmw.vmx['memsize'] = machine['ram'] + vmw.vmx['numvcpus'] = machine['vcpu'] + if machine['nested'] == true + vmw.vmx['vhv.enable'] = 'TRUE' + end #if machine['nested'] + end # srv.vm.provider 'vmware_fusion' + + # Configure CPU & RAM per settings in machines.yml (VirtualBox) + srv.vm.provider 'virtualbox' do |vb, override| + vb.memory = machine['ram'] + vb.cpus = machine['vcpu'] + override.vm.box = machine['box']['vb'] + vb.customize ['modifyvm', :id, '--nictype1', 'virtio'] + vb.customize ['modifyvm', :id, '--nictype2', 'virtio'] + end # srv.vm.provider 'virtualbox' + + # Configure CPU & RAM per settings in machines.yml (Libvirt) + srv.vm.provider 'libvirt' do |lv,override| + lv.memory = machine['ram'] + lv.cpus = machine['vcpu'] + override.vm.box = machine['box']['lv'] + if machine['nested'] == true + lv.nested = true + end # if machine['nested'] + end # srv.vm.provider 'libvirt' + end # config.vm.define + end # machines.each + + # Provision the VM with Ansible + config.vm.provision 'ansible' do |ansible| + ansible.playbook = 'provision.yml' + end # config.vm.provision +end # Vagrant.configure diff --git a/pulumi/ansible.cfg b/pulumi/ansible.cfg new file mode 100644 index 0000000..71bbae5 --- /dev/null +++ b/pulumi/ansible.cfg @@ -0,0 +1,6 @@ +[defaults] +inventory = ./.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory +private_key_file = ~/.vagrant.d/insecure_private_key +remote_user = vagrant +host_key_checking = False +retry_files_enabled = False diff --git a/pulumi/js-simple-ec2/Pulumi.yaml b/pulumi/js-simple-ec2/Pulumi.yaml new file mode 100644 index 0000000..598d604 --- /dev/null +++ b/pulumi/js-simple-ec2/Pulumi.yaml @@ -0,0 +1,8 @@ +name: ec2instance +runtime: nodejs +description: Basic example of an AWS EC2 instance +template: + config: + aws:region: + description: The AWS region to deploy into + default: us-west-2 diff --git a/pulumi/js-simple-ec2/index.js b/pulumi/js-simple-ec2/index.js new file mode 100644 index 0000000..1016076 --- /dev/null +++ b/pulumi/js-simple-ec2/index.js @@ -0,0 +1,34 @@ +"use strict"; + +const aws = require("@pulumi/aws"); + +// Specify instance size/type +let size = "t2.micro"; // t2.micro is available in the AWS free tier + +// Specify AMI +let ami = "ami-c55673a0"; // AMI for Ubuntu 16.04 in us-west-2 (Oregon) + +// Create a new security group for port 80 +let group = new aws.ec2.SecurityGroup("pulumi-secgrp", { + ingress: [ + { protocol: "tcp", fromPort: 22, toPort: 22, cidrBlocks: ["0.0.0.0/0"] }, + { protocol: "tcp", fromPort: 80, toPort: 80, cidrBlocks: ["0.0.0.0/0"] }, + ], +}); + +// (optional) create a simple web server using the startup script for the instance +let userData = +`#!/bin/bash +echo "Hello, World!" > index.html +nohup python -m SimpleHTTPServer 80 &`; + +let server = new aws.ec2.Instance("pulumi-ubuntu", { + tags: { "Name": "pulumi-ubuntu" }, + instanceType: size, + securityGroups: [ group.name ], // reference the group object above + ami: ami, + userData: userData // start a simple web server +}); + +exports.publicIp = server.publicIp; +exports.publicHostName = server.publicDns; diff --git a/pulumi/js-simple-ec2/package.json b/pulumi/js-simple-ec2/package.json new file mode 100644 index 0000000..af85126 --- /dev/null +++ b/pulumi/js-simple-ec2/package.json @@ -0,0 +1,9 @@ +{ + "name": "ec2instance", + "version": "0.1.0", + "main": "index.js", + "dependencies": { + "@pulumi/pulumi": "latest", + "@pulumi/aws": "latest" + } +} diff --git a/pulumi/js-simple-ec2/playbook.yml b/pulumi/js-simple-ec2/playbook.yml new file mode 100644 index 0000000..0da8539 --- /dev/null +++ b/pulumi/js-simple-ec2/playbook.yml @@ -0,0 +1,22 @@ +--- +- hosts: "all" + remote_user: "vagrant" + + tasks: + - name: "Create directory to store files" + file: + state: "directory" + path: "/home/vagrant/pulumi" + owner: "vagrant" + group: "vagrant" + + - name: "Copy files into sandbox environment" + copy: + src: "{{ item }}" + dest: "/home/vagrant/pulumi" + owner: "vagrant" + group: "vagrant" + with_items: + - "index.js" + - "package.json" + - "Pulumi.yaml" diff --git a/pulumi/machines.yml b/pulumi/machines.yml new file mode 100644 index 0000000..aa17723 --- /dev/null +++ b/pulumi/machines.yml @@ -0,0 +1,9 @@ +--- +- box: + vmw: "generic/ubuntu1604" + vb: "ubuntu/xenial64" + lv: "generic/ubuntu1604" + name: "xenial" + nested: false + ram: "1024" + vcpu: "1" diff --git a/pulumi/provision.yml b/pulumi/provision.yml new file mode 100644 index 0000000..9de4dc4 --- /dev/null +++ b/pulumi/provision.yml @@ -0,0 +1,59 @@ +--- +- hosts: "all" + become: "yes" + remote_user: "vagrant" + gather_facts: false + + pre_tasks: + - name: "Install Python 2 if not present" + raw: "apt -qqy update; apt -qqy install python" + register: output + retries: 3 + delay: 3 + until: output.rc == 0 + + tasks: + - name: "Ensure /usr/local/bin exists" + file: + state: "directory" + path: "/usr/local/bin" + owner: "root" + group: "root" + + - name: "Check for existence of Pulumi binary in /usr/local/bin" + stat: + path: "/usr/local/bin/pulumi" + register: pulumi_present + + - name: Install NodeJS + apt: + state: "present" + name: "{{ item }}" + with_items: + - nodejs + - npm + + - block: + - name: "Download and extract Pulumi 0.17.5" + unarchive: + src: "https://get.pulumi.com/releases/sdk/pulumi-v0.17.5-linux-x64.tar.gz" + remote_src: true + dest: "/tmp" + owner: "root" + group: "root" + - name: "Move binaries to /usr/local/bin" + copy: + src: "/tmp/pulumi/{{ item }}" + remote_src: true + dest: "/usr/local/bin" + owner: "root" + group: "root" + mode: 0755 + with_items: + - "pulumi" + - "pulumi-language-go" + - "pulumi-language-nodejs" + - "pulumi-language-python" + - "pulumi-language-python-exec" + - "pulumi-resource-pulumi-nodejs" + when: pulumi_present.stat.exists == False