Last active
April 27, 2017 22:11
-
-
Save tylerpace/db6aee1fe5f49624892994f2fe4abff6 to your computer and use it in GitHub Desktop.
DockerCon 2017 Demo Commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
RED='\033[0;31m' | |
ORANGE='\033[0;33m' | |
GREEN='\033[0;32m' | |
END='\033[0m' | |
CONTAINER_NAME="inventory-${RANDOM}" | |
SOURCE_IMAGE="${1}" | |
DESTINATION_IMAGE="${2}" | |
if [ -z ${SOURCE_IMAGE} ]; then | |
echo -e "----> ${RED}You must provide at least one argument${END}" | |
exit | |
fi | |
docker inspect puppet-inventory 2>&1 >/dev/null | |
if [ $? -eq 1 ]; then | |
echo -e "----> ${ORANGE}Need a puppet-inventory volume to be created${END}" | |
docker run --name puppet-inventory puppet/puppet-inventory 2>&1 >/dev/null | |
else | |
echo -e "----> ${GREEN}Using existing puppet-inventory volume${END}" | |
fi | |
if [ "${SOURCE_IMAGE}" = "--help" ]; then | |
echo "Add a static inventory to a Docker image | |
Usage: | |
${0} <source> [<destination>] | |
If no destination is passed then the original source image will be modified | |
and replaced. | |
This script will work for images based on most Linux-based images apart from those | |
using busybox or alpine derivates." | |
exit | |
fi | |
# the second parameter is optional. If not passed then | |
# override the source image | |
if [ -z ${DESTINATION_IMAGE} ]; then | |
echo -e "----> ${ORANGE}The destination image will be the same as the source image${END}" | |
DESTINATION_IMAGE=${SOURCE_IMAGE} | |
fi | |
echo -e "----> ${GREEN}Generating inventory for image ${SOURCE_IMAGE}${END}" | |
docker run --volumes-from=puppet-inventory --name ${CONTAINER_NAME} ${SOURCE_IMAGE} /opt/puppetlabs/bin/puppet inventory > inventory.json | |
echo -e "----> ${GREEN}Saving inventory to temporary image ${CONTAINER_NAME}${END}" | |
docker cp inventory.json ${CONTAINER_NAME}:/inventory.json | |
echo -e "----> ${GREEN}Committing new image to ${DESTINATION_IMAGE}${END}" | |
docker commit ${CONTAINER_NAME} ${DESTINATION_IMAGE} | |
echo -e "----> ${GREEN}Cleaning up${END}" | |
docker rm ${CONTAINER_NAME} >> /dev/null | |
rm inventory.json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Likely Commands | |
docker run --name puppet-inventory puppet/puppet-inventory | |
docker run --rm -it --volumes-from=puppet-inventory centos /opt/puppetlabs/bin/puppet inventory | |
docker run --rm -it --volumes-from=puppet-inventory centos /opt/puppetlabs/bin/puppet inventory > inventory.json | |
cat inventory.json | jq '.facts.operatingsystem' | |
cat inventory.json | jq '.resources[] | .resource + ": " + .title' | |
cat inventory.json | jq '.resources[] | select(.resource == "package") | [.title, .provider, .versions[0]]' | |
cat inventory.json | jq -r '.resources[] | select(.resource == "package") | [.title, .provider, .versions[0]] | @csv' > packages.csv | |
python csv2html.py | |
# Random Other Stuff I'm Playing With | |
docker run --rm -it --volumes-from=puppet-inventory centos /opt/puppetlabs/bin/puppet inventory facts | |
docker run --rm -it --volumes-from=puppet-inventory centos /opt/puppetlabs/bin/puppet inventory resources | |
docker run --rm --name puppet-inventory puppet/puppet-inventory cat /inventory.json | jq '.facts.operatingsystem' | |
docker run --rm centos-inventory cat /inventory.json | jq '.facts' | |
docker run --rm centos-inventory cat /inventory.json | jq '.facts.operatingsystem' | |
docker run --rm centos-inventory cat /inventory.json | jq '.resources[] | .resource + ": " + .title' | |
docker run --rm centos-inventory cat /inventory.json > inventory.json | |
cat inventory.json | jq '.resources[]' | |
cat inventory.json | jq '.resources[] | .resource + ": " + .title' | |
cat inventory.json | jq '.resources[] | .versions + ": " + .title' | |
cat inventory.json | jq '.resources[] | .resource + ": " + .title' | grep 'ssl' | |
cat inventory.json | jq '.resources[] | select(.title == "openssl-libs")' | |
cat inventory.json | jq '.resources[] | select(.resource == "package") | [.title, .provider, .versions[0]]' | |
cat inventory.json | jq -r '.resources[] | select(.resource == "package") | [.title, .provider, .versions[0]] | @csv' > packages.csv |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# modified from http://victoriaxie.com/eng/2015/9/10/a-simple-way-to-convert-csv-data-into-a-sortable-bootstraphtml-table-using-python | |
import csv | |
def main(csv_file='packages.csv'): | |
csv_data = open_csv(csv_file) | |
write_html_from_csv_data(csv_data) | |
def open_csv(csv_file): | |
with open(csv_file, 'r') as csv_data: | |
return list(csv.reader(csv_data)) | |
def write_html_from_csv_data(csv_data, output='output.html'): | |
with open(output, 'w') as html: | |
html.write('''<!-- Latest compiled and minified CSS --> | |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.8.1/bootstrap-table.min.css"> | |
<!-- Latest compiled and minified CSS --> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> | |
''') | |
html.write('<table data-toggle = "table" data-pagination = "true">\r') | |
html.write('\t<thead>\r\t\t<tr>\r') | |
for col in ['package', 'provider', 'version']: | |
html.write('\t\t\t<th data-sortable="true">{0}</th>\r'.format(col)) | |
html.write('\t\t</tr>\r\t</thead>\r') | |
html.write('\t<tbody>\r') | |
for row in csv_data: | |
html.write('\t\t<tr>\r') | |
for col in row: | |
html.write('\t\t\t<td>{0}</td>\r'.format(col.strip('\\"'))) | |
html.write('\t\t</tr>\r') | |
html.write('\t</tbody>\r') | |
html.write('</table>\r') | |
html.write(''' | |
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> | |
<!-- Latest compiled and minified JavaScript --> | |
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> | |
<!-- Latest compiled and minified JavaScript --> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.8.1/bootstrap-table.min.js"></script> | |
''') | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment