Skip to content

Commit e2ff793

Browse files
Initial Vagrant VM for clouddata server
0 parents  commit e2ff793

6 files changed

+242
-0
lines changed

Vagrantfile

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# -*- mode: ruby -*-
2+
# vi: set ft=ruby :
3+
4+
# All Vagrant configuration is done below. The "2" in Vagrant.configure
5+
# configures the configuration version (we support older styles for
6+
# backwards compatibility). Please don't change it unless you know what
7+
# you're doing.
8+
Vagrant.configure(2) do |config|
9+
# The most common configuration options are documented and commented below.
10+
# For a complete reference, please see the online documentation at
11+
# https://docs.vagrantup.com.
12+
13+
# Every Vagrant development environment requires a box. You can search for
14+
# boxes at https://atlas.hashicorp.com/search.
15+
config.vm.box = "ubuntu/xenial64"
16+
17+
# Disable automatic box update checking. If you disable this, then
18+
# boxes will only be checked for updates when the user runs
19+
# `vagrant box outdated`. This is not recommended.
20+
# config.vm.box_check_update = false
21+
22+
# Create a forwarded port mapping which allows access to a specific port
23+
# within the machine from a port on the host machine. In the example below,
24+
# accessing "localhost:8080" will access port 80 on the guest machine.
25+
config.vm.network "forwarded_port", guest: 80, host: 8080
26+
27+
# internal ports -- to be made private
28+
#config.vm.network "forwarded_port", guest: 3000, host: 3000
29+
#config.vm.network "forwarded_port", guest: 8083, host: 8083
30+
31+
# Create a private network, which allows host-only access to the machine
32+
# using a specific IP.
33+
# config.vm.network "private_network", ip: "192.168.33.10"
34+
35+
# Create a public network, which generally matched to bridged network.
36+
# Bridged networks make the machine appear as another physical device on
37+
# your network.
38+
# config.vm.network "public_network"
39+
40+
# Share an additional folder to the guest VM. The first argument is
41+
# the path on the host to the actual folder. The second argument is
42+
# the path on the guest to mount the folder. And the optional third
43+
# argument is a set of non-required options.
44+
config.vm.synced_folder ".", "/vagrant"
45+
46+
# Provider-specific configuration so you can fine-tune various
47+
# backing providers for Vagrant. These expose provider-specific options.
48+
# Example for VirtualBox:
49+
#
50+
# config.vm.provider "virtualbox" do |vb|
51+
# # Display the VirtualBox GUI when booting the machine
52+
# vb.gui = true
53+
#
54+
# # Customize the amount of memory on the VM:
55+
# vb.memory = "1024"
56+
# end
57+
#
58+
# View the documentation for the provider you are using for more
59+
# information on available options.
60+
61+
# Enable provisioning with a shell script.
62+
config.vm.provision "shell", path: "setup.sh"
63+
64+
end

clouddata_server.program.conf

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[program:clouddataserver]
2+
#environment=PYTHONPATH=/home/_USER_/.local/lib/python2.7/site-packages/
3+
directory=/home/ubuntu/
4+
command=/usr/bin/python /home/ubuntu/clouddata_server.py
5+
user=ubuntu
6+
autostart=true
7+
autorestart=true
8+
stdout_logfile=/home/ubuntu/clouddata_server.log
9+
stdout_logfile_maxbytes=10MB
10+
stderr_logfile=/home/ubuntu/clouddata_server.errlog
11+
stderr_logfile_maxbytes=10MB

clouddata_server.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/python
2+
3+
#============================ define ==========================================
4+
5+
DFLT_PORT = 8080
6+
INFLUX_HOST = 'localhost'
7+
INFLUX_PORT = 8086
8+
INFLUX_DBNAME = 'grafana'
9+
10+
#============================ imports =========================================
11+
12+
from bottle import post,request,run
13+
import influxdb
14+
15+
print 'CloudData Server'
16+
17+
influxClient = influxdb.client.InfluxDBClient(
18+
host = INFLUX_HOST,
19+
port = INFLUX_PORT,
20+
database = INFLUX_DBNAME,
21+
)
22+
23+
@post('/oap')
24+
def root_post():
25+
mac = request.json['mac']
26+
temperature = request.json['temperature']
27+
influxClient.write_points(
28+
[
29+
{
30+
"measurement": "temperature",
31+
"tags": {
32+
"mac": mac,
33+
},
34+
"fields": {
35+
"value": temperature,
36+
}
37+
},
38+
]
39+
)
40+
print 'received mac={0} temperature={1}'.format(mac,temperature)
41+
42+
print 'Server started on port {0}'.format(DFLT_PORT)
43+
run(host='0.0.0.0', port=DFLT_PORT, quiet=True)

clouddata_server.site.conf

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
server {
2+
listen 80 default_server;
3+
listen [::]:80 default_server;
4+
5+
root /var/www/html;
6+
7+
index index.html index.htm index.nginx-debian.html;
8+
9+
server_name _;
10+
11+
location /api/v1 {
12+
proxy_pass http://localhost:8080;
13+
rewrite ^/api/v1/(.*) /$1 break;
14+
proxy_set_header Host $host;
15+
}
16+
17+
location / {
18+
proxy_pass http://localhost:3000;
19+
rewrite ^/(.*) /$1 break;
20+
proxy_set_header Host $host;
21+
}
22+
}

grafanadb.influx

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# DDL
2+
3+
CREATE DATABASE grafana
4+
CREATE RETENTION POLICY twelve_h_only ON grafana DURATION 12h REPLICATION 1 DEFAULT
5+
6+
# DML
7+
# CONTEXT-DATABASE: grafana
8+
# CONTEXT-RETENTION-POLICY: twelve_h_only
9+
10+
temperature,mac=11-11-11-11-11-11-11-11 value=29
11+
temperature,mac=22-22-22-22-22-22-22-22 value=21

setup.sh

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/bin/bash
2+
3+
# Update system timezone
4+
rm -f /etc/localtime
5+
ln -s /usr/share/zoneinfo/US/Pacific /etc/localtime
6+
7+
# Remove unused packages from original distribution
8+
sudo apt-get remove -y puppet chef
9+
10+
# ----------------------------------------------------------------------
11+
# Get up-to-date
12+
sudo apt-get update
13+
sudo apt-get upgrade -y
14+
sudo apt-get autoremove -y
15+
16+
# guest utils for VirtualBox are missing in 16.04
17+
sudo apt-get install -y --no-install-recommends virtualbox-guest-utils
18+
19+
# Install required packages
20+
sudo apt-get install -y openssh-server
21+
sudo apt-get install -y python-pip
22+
sudo apt-get install -y supervisor
23+
sudo apt-get install -y curl
24+
sudo apt-get install -y unzip
25+
sudo apt-get install -y stunnel4
26+
sudo apt-get install -y nginx
27+
28+
sudo systemctl enable supervisor.service
29+
30+
sudo pip install requests
31+
32+
# ----------------------------------------------------------------------
33+
# Install and start InfluxDB
34+
35+
curl -sL https://repos.influxdata.com/influxdb.key | sudo apt-key add -
36+
source /etc/lsb-release
37+
echo "deb https://repos.influxdata.com/${DISTRIB_ID,,} ${DISTRIB_CODENAME} stable" | sudo tee /etc/apt/sources.list.d/influxdb.list
38+
sudo apt-get update && sudo apt-get install -y influxdb
39+
40+
sudo systemctl daemon-reload
41+
sudo systemctl unmask influxdb
42+
sudo systemctl start influxdb
43+
sudo systemctl enable influxdb.service
44+
45+
# configure the grafana database
46+
influx -import -path=/vagrant/grafanadb.influx
47+
48+
# ----------------------------------------------------------------------
49+
# Install and start grafana
50+
51+
curl -sL https://packagecloud.io/gpg.key | sudo apt-key add -
52+
# grafana says use wheezy even for recent Ubuntu distributions
53+
echo "deb https://packagecloud.io/grafana/stable/debian/ wheezy main" | sudo tee /etc/apt/sources.list.d/grafana.list
54+
sudo apt-get update && sudo apt-get install -y adduser libfontconfig grafana
55+
56+
sudo systemctl daemon-reload
57+
sudo systemctl start grafana-server
58+
#systemctl status grafana-server
59+
sudo systemctl enable grafana-server.service
60+
61+
# ----------------------------------------------------------------------
62+
# configure nginx
63+
64+
sudo cp /vagrant/clouddata_server.site.conf /etc/nginx/sites-available/clouddata_server.conf
65+
sudo rm /etc/nginx/sites-enabled/default
66+
sudo ln -s /etc/nginx/sites-available/clouddata_server.conf /etc/nginx/sites-enabled/default
67+
68+
sudo nginx -s reload
69+
70+
# ----------------------------------------------------------------------
71+
# configure clouddata_server
72+
73+
# clouddata_server requirements
74+
sudo pip install influxdb
75+
sudo pip install bottle
76+
77+
# note: clouddata_server.py comes from the SmartMeshSDK source
78+
# http://dust-jenkins:8080/job/SmartMeshSDK/lastSuccessfulBuild/artifact/SmartMeshSDKTool/gen/SmartMeshSDK-1.0.7.140.zip
79+
80+
cp /vagrant/clouddata_server.py /home/ubuntu/clouddata_server.py
81+
82+
sudo cp /vagrant/clouddata_server.program.conf /etc/supervisor/conf.d/clouddata_server.conf
83+
sudo systemctl restart supervisor
84+
85+
# ----------------------------------------------------------------------
86+
# other configurations -- manually set up
87+
# - update hostname to clouddata
88+
# - grafana: configure influxdb data source
89+
# - grafana: dashboard creation, set as default
90+
# - grafana: enable anonymous access
91+
# - iptables firewall

0 commit comments

Comments
 (0)