Skip to content

Instantly share code, notes, and snippets.

@zoeyronain
Created December 15, 2017 14:29
Show Gist options
  • Save zoeyronain/3bd1f8121e2a97b2f044d94c55b207c5 to your computer and use it in GitHub Desktop.
Save zoeyronain/3bd1f8121e2a97b2f044d94c55b207c5 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import pycurl
import socket
import time
from twindb_cloudflare.twindb_cloudflare import CloudFlare, CloudFlareException
from StringIO import StringIO
from urllib import urlencode
import requests
import simplejson as json
import time
import os
from ansible import *
import logging
import pprint
# Cloudflare API information found in your account
CLOUDFLARE_EMAIL = ""
CLOUDFLARE_AUTH_KEY = ""
cf = CloudFlare(CLOUDFLARE_EMAIL, CLOUDFLARE_AUTH_KEY)
# Create Vultr VPS using default values
url = 'https://api.vultr.com/v1/server/create'
headers = {'API-Key': ''}
payload = {'DCID': '2', 'VPSPLANID': '201', 'OSID': '252', 'enable_ipv6': 'yes', 'SSHKEYID': '57d133b966663', 'FIREWALLGROUPID': 'f3e4770c', 'label': 'letsnotbestupid', 'hostname': 'testlol'}
r = requests.post(url, headers=headers, data=payload).json()
SUBID = r["SUBID"]
print(SUBID)
print("VPS Should be created successfully, waiting 60 seconds to provision")
time.sleep(60)
url = "https://api.vultr.com/v1/server/list_ipv4"
querystring = {"SUBID":SUBID}
SUBID = querystring["SUBID"]
headers = {
'API-Key': "",
'Cache-Control': "no-cache"
}
response = requests.get(url, headers=headers, params=querystring).json()
ipv4 = response[SUBID][0]['ip']
print(ipv4)
url = "https://api.vultr.com/v1/server/list_ipv6"
querystring = {"SUBID":SUBID}
headers = {
'API-Key': "",
'Cache-Control': "no-cache"
}
response = requests.get(url, headers=headers, params=querystring).json()
ipv6 = response[SUBID][0]['ip']
print(ipv6)
domain = str(raw_input("What is the domain name? "))
cf.create_dns_record('@', domain, ipv4)
cf.create_dns_record('www', domain, ipv4)
cf.create_dns_record('@', domain, ipv6, record_type="AAAA")
cf.create_dns_record('www', domain, ipv6, record_type="AAAA")
print("Cloudflare records added successfully")
print("Writing IP's to a file for later use configuring the server")
f = open("./ansible/ips.txt", 'w')
f.write("ipv4="+ipv4 + '\n' + "ipv6="+ipv6)
f.close()
# newData = ''
# with open('./ansible/variables.yaml', 'r') as f:
# for line in f:
# if line.strip().startswith('ipv4:'):
# newData += line.replace('ipv4:' + ipv4)
# elif line.strip().startswith('ipv6:'):
# newData += line.replace('ipv6:' + ipv6)
#
# with open("./ansible/variables.yaml", 'w') as f:
# f.write(newData)
f = open("./ansible/hosts", 'w')
f.write("[VPS]" + '\n'+ ipv4)
f.close()
print("Server created successfully, DNS updated, proceeding to configure the new server as a LEMP host, and downloading and configuring Wordpresss.")
print("Invoking Ansible to configure wordpress server, this may take a while.")
print("Waiting 30 seconds for any last minute provisioning to finish")
time.sleep(30)
os.system('ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -i ./ansible/hosts ./ansible/playbook.yaml -vvv --key-file "~/.ssh/id_rsa "')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment