Skip to content

Commit

Permalink
Initial commit ☄️
Browse files Browse the repository at this point in the history
  • Loading branch information
megallo committed Jun 19, 2018
0 parents commit bd54630
Show file tree
Hide file tree
Showing 12 changed files with 907 additions and 0 deletions.
41 changes: 41 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

__pycache__/
*.py[cod]

# Distribution / packaging
build/
develop-eggs/
dist/
eggs/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# IDEA project files
*.iml
*.ipr
*.iws
*.ids
.idea/
out/

# Eclipse project files
.settings/
.classpath
.project

## generic files to ignore
*~
*.lock
*.DS_Store
*.swp
*.out
*.bak

*.bz2
*.pyc
*.deb
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2018 Megan Galloway

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 changes: 30 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
PYTHON_MAIN=pfucking_shell_scripts/pfss.py
FINAL_EXECUTABLE=pfss
BUILD_DIR=build
PYTHON_INTERPRETER=python3

.PHONY: all clean build

all: clean build

clean:
@rm -rf *.pyc
@echo "Project .pyc's removed."
@rm -rf $(BUILD_DIR)
@echo "Build directory removed."

build: build/_virtualenv
@rm -f $(BUILD_DIR)/$(FINAL_EXECUTABLE)
@sh -c '. $(BUILD_DIR)/_virtualenv/bin/activate; $(PYTHON_INTERPRETER) eggsecute.py $(PYTHON_MAIN) $(BUILD_DIR)/$(FINAL_EXECUTABLE)'
@chmod a+x $(BUILD_DIR)/$(FINAL_EXECUTABLE)
@echo "Package created."

build/_virtualenv:
@command -v virtualenv >/dev/null 2>&1 || { echo >&2 "This build requires virtualenv to be installed. Aborting."; exit 1; }
@mkdir -p $(BUILD_DIR)
@if [ -d $(BUILD_DIR)/_virtualenv ]; then \
echo "Existing virtualenv found. Skipping virtualenv creation."; \
else \
virtualenv -p `which $(PYTHON_INTERPRETER)` $(BUILD_DIR)/_virtualenv; \
sh -c '. $(BUILD_DIR)/_virtualenv/bin/activate; pip install .'; \
fi
157 changes: 157 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
# Pfucking Shell Scripts

This is a straight-up rewrite of [Fucking Shell Scripts](https://github.com/brandonhilkert/fucking_shell_scripts) in Python.

"The easiest, most common sense server configuration management tool...because you just use fucking shell scripts."

# Features

* AWS-only. EC2-classic only. More features will come when I need them.
* This tool was originally designed to be insanely easy to use, and I left it that way

# What do?

Use this tool to configure an AWS base AMI by running shell scripts on it. It will spin up the instance, copy scripts to it, and run them. That's it.

# Installation
PFSS is a command-line tool that uses [Boto](https://github.com/boto/boto3). It has been tested on Ubuntu 15.10 with Python 3.4.3.

You will need to set up AWS auth as per the [boto documentation](https://boto3.readthedocs.io/en/latest/guide/quickstart.html#configuration).

Grab the latest version and set it to executable like so:
```sh
sudo curl -o /usr/local/bin/pfss -L "https://github.com/megallo/pfss/releases/download/v1.0.0/pfss" && \
sudo chmod +x /usr/local/bin/pfss
```

# Setup

### Step 1: Create a project directory

```sh
mkdir config_management
```

Folder structure:

* `/servers` _(required)_ - yaml server definitions _(see example below)_

* `/scripts` _(required)_ - the shell scripts that will configure your servers _(see example below)_


An example folder structure:

```Shell
./config_management
├── scripts
│ ├── apt.sh
│ ├── deploy_key.sh
│ ├── git.sh
│ ├── redis.sh
│ ├── ruby2.sh
│ ├── rubygems.sh
│ ├── search_service_code.sh
│ └── search_service_env.sh
└── servers
├── defaults.yml
└── search-server.yml
```


### Step 2: Create a server definition file

The server definition file defines how to build a type of server. Server definitions override settings in `defaults.yml`.

```YAML
# servers/search-server.yml
##################################################
# This file defines how to build our search server
##################################################

name: search-server
size: c1.xlarge
availability_zone: us-east-1d
image: ami-90374bf9
key_name: pd-app-server
private_key_path: /Users/yourname/.ssh/pd-app-server
security_groups: search-service # override the security_groups defined in defaults.yml

###########################################
# Scripts needed to build the search server
###########################################

scripts:
- scripts/apt.sh
- scripts/search_service_env.sh
- scripts/git.sh
- scripts/ruby2.sh
- scripts/rubygems.sh
- scripts/redis.sh
- scripts/deploy_key.sh
```
`servers/defaults.yml`has the same structure and keys a server definition file, **except**, you cannot define scripts or files.

```YAML
# servers/defaults.yml
################################
# This file defines our defaults
################################
security_groups: simple-group
size: c1.medium
image: ami-e76ac58e
availability_zone: us-east-1d
key_name: global-key
```

### Step 3: Add shell scripts that configure the server

Seriously...just write shell scripts.

Want to install Ruby 2? Here's an example:

```Shell
#!/bin/sh
#
# scripts/ruby2.sh
#
sudo apt-get -y install build-essential zlib1g-dev libssl-dev libreadline6-dev libyaml-dev
cd /tmp
wget http://ftp.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p247.tar.gz
tar -xzf ruby-2.0.0-p247.tar.gz
cd ruby-2.0.0-p247
./configure --prefix=/usr/local
make
sudo make install
rm -rf /tmp/ruby*
```

### Step 4: Build/configure your server

```Shell
pfss search-server
```

This command does 2 things:

1. Builds the new server
2. Runs the scripts configuration

**HOLY SHIT! THAT WAS EASY.**

## Development
If you don't like my binary and want to build your own, you must have `virtualenv` installed. Then run
```sh
make all
```

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request

102 changes: 102 additions & 0 deletions eggsecute.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/usr/bin/python

## Copyright 2016 Ray Holder
##
## Licensed 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.

import os
import sys
import zipfile


def collect_single_module_file(module_name):
"""Return a list of tuples of (absolute_file_path, zip_target_path) for a single module file, like six"""
loaded_module = __import__(module_name, globals(), locals(), [], 0)
module_file = loaded_module.__file__
file_path = os.path.basename(module_file)

return [(module_file, file_path)]


def collect_module_files(module_name, relative_path_in_module):
"""Return a list of tuples of (absolute_file_path, zip_target_path)"""
loaded_module = __import__(module_name, globals(), locals(), [], 0)
module_path = os.path.dirname(loaded_module.__file__)
if len(relative_path_in_module) == 0:
# walk the whole module
data_path = module_path
else:
# only walk the relative path in the module
data_path = module_path + '/' + relative_path_in_module

file_data = []
for dirpath, dirnames, filenames in os.walk(data_path):
for filename in filenames:
file_path = dirpath + '/' + filename
target_path = module_name + dirpath.replace(module_path, '') + '/' + filename
file_data.append((file_path, target_path))
return file_data

def main(script_path, output_path):
if os.path.exists(output_path):
sys.stderr.write("output path '%s' exists; refusing to overwrite\n" % output_path)
return 1

# tack Python header onto a file, zip file parsers ignore everything up until PK magic string
outfile = open(output_path, 'w+b')
outfile.write(b"#!/usr/bin/env python3\n")

# make sure we flush, since we'll be writing zip data right after this
outfile.flush()

# create the zip file stream
outzip = zipfile.ZipFile(outfile, 'a', zipfile.ZIP_DEFLATED)

# this is the first thing that Python finds to run, __main__ is special
outzip.write(script_path, "__main__.py")

# hack to explicitly add everything
module_files = []
module_files.extend(collect_module_files('pfucking_shell_scripts', ''))
module_files.extend(collect_module_files('boto3', ''))
module_files.extend(collect_module_files('botocore', ''))
module_files.extend(collect_module_files('dateutil', ''))
module_files.extend(collect_module_files('jmespath', ''))
module_files.extend(collect_module_files('click', ''))
module_files.extend(collect_module_files('future', ''))
module_files.extend(collect_module_files('ply', ''))
module_files.extend(collect_module_files('pureyaml', ''))
module_files.extend(collect_single_module_file('sh'))
module_files.extend(collect_single_module_file('six'))

# filter out everything but files ending with .py .json .pem .out
filtered_files = [x for x in module_files if x[1].endswith(".py") or x[1].endswith(".json") or x[1].endswith(".pem") or x[1].endswith(".out")]

for source_path, relative_destination_path in set(filtered_files):
outzip.write(source_path, relative_destination_path)
outzip.close()
outfile.close()

os.chmod(output_path, 0o755)

return 0


if __name__ == "__main__":
if len(sys.argv) != 3:
sys.stderr.write("eggsecute <main_function_file> <output_package_file>\n")
sys.exit(1)
script_path = sys.argv[1]
output_path = sys.argv[2]

sys.exit(main(script_path, output_path))
Empty file.
Loading

0 comments on commit bd54630

Please sign in to comment.