From 08e095e820e90f5926c65cd31f651eafb052819c Mon Sep 17 00:00:00 2001 From: Paul Hendryx Date: Sun, 20 Nov 2016 15:29:08 -0500 Subject: [PATCH 1/2] initial commit --- Dockerfile | 165 +++++++++++++++++++++ app/.ruby-gemset | 1 + app/.ruby-version | 1 + app/Gemfile | 9 ++ app/Gemfile.lock | 28 ++++ app/config/settings.yml.example | 3 + app/stats.rb | 40 +++++ root/etc/cont-init.d/50-grafana.sh | 37 +++++ root/etc/cont-init.d/51-influxdb.sh | 25 ++++ root/etc/cont-init.d/52-dockerhub_stats.sh | 9 ++ root/etc/services.d/grafana/run | 3 + root/etc/services.d/influxdb/run | 3 + 12 files changed, 324 insertions(+) create mode 100644 Dockerfile create mode 100644 app/.ruby-gemset create mode 100644 app/.ruby-version create mode 100644 app/Gemfile create mode 100644 app/Gemfile.lock create mode 100644 app/config/settings.yml.example create mode 100644 app/stats.rb create mode 100644 root/etc/cont-init.d/50-grafana.sh create mode 100644 root/etc/cont-init.d/51-influxdb.sh create mode 100644 root/etc/cont-init.d/52-dockerhub_stats.sh create mode 100644 root/etc/services.d/grafana/run create mode 100644 root/etc/services.d/influxdb/run diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f347c88 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,165 @@ +FROM lsiobase/xenial +MAINTAINER phendryx + +# set version label +#ARG BUILD_DATE +#ARG VERSION +#LABEL build_version="Linuxserver.io version:- ${VERSION} Build-date:- ${BUILD_DATE}" + +# environment settings +ARG DEBIAN_FRONTEND="noninteractive" + +# build packages as variable +ARG BUILD_PACKAGES="\ + apt-transport-https \ + binutils \ + bzip2 \ + cpp \ + cpp-5 \ + dh-python \ + distro-info-data \ + g++ \ + g++-5 \ + gcc \ + gcc-5 \ + git \ + git-core \ + git-man \ + icu-devtools \ + libasan2 \ + libatomic1 \ + libc-dev-bin \ + libc6-dev \ + libcc1-0 \ + libcilkrts5 \ + liberror-perl \ + libgcc-5-dev \ + libgdbm3 \ + libgmp-dev \ + libgmpxx4ldbl \ + libgomp1 \ + libicu-dev \ + libicu55 \ + libisl15 \ + libitm1 \ + liblsan0 \ + libmpc3 \ + libmpdec2 \ + libmpfr4 \ + libmpx0 \ + libperl5.22 \ + libpng12-0 \ + libpython3-stdlib \ + libpython3.5-minimal \ + libpython3.5-stdlib \ + libquadmath0 \ + libruby2.3 \ + libstdc++-5-dev \ + libtcl8.6 \ + libtcltk-ruby \ + libtk8.6 \ + libtsan0 \ + libubsan0 \ + libx11-6 \ + libx11-data \ + libxau6 \ + libxcb1 \ + libxdmcp6 \ + libxext6 \ + libxml2 \ + libxml2-dev \ + libxrender1 \ + libxslt1-dev \ + libxslt1.1 \ + libxss1 \ + libyaml-0-2 \ + linux-libc-dev \ + lsb-release \ + make \ + mime-support \ + perl \ + perl-modules-5.22 \ + python3 \ + python3-minimal \ + python3.5 \ + python3.5-minimal \ + rake \ + ri \ + ruby \ + ruby-dev \ + ruby-did-you-mean \ + ruby-full \ + ruby-minitest \ + ruby-net-telnet \ + ruby-power-assert \ + ruby-test-unit \ + ruby2.3 \ + ruby2.3-dev \ + ruby2.3-doc \ + ruby2.3-tcltk \ + rubygems-integration \ + wget \ + x11-common \ + zlib1g-dev \ + fontconfig-config \ + fonts-dejavu-core \ + libexpat1 \ + libfontconfig1 \ + libfontconfig \ + libfreetype6 \ + libpng12-0 \ + ucf" + +# copy app and excludes +COPY root/ / +COPY app/ /opt/dockerhub-stats/ + +# install build packages +RUN \ + apt-get update && \ + apt-get install -y \ + --no-install-recommends \ + $BUILD_PACKAGES && \ + + + +# install ruby app gems +cd /opt/dockerhub-stats/ && \ + echo 'gem: --no-document' > \ + /etc/gemrc && \ + gem install bundler && \ + bundle install && \ + +# clean up + apt-get purge -y --auto-remove \ + $BUILD_PACKAGES && \ + +# install runtime packages + apt-get install -y \ + --no-install-recommends \ + libfontconfig1 \ + libfreetype6 \ + libpng12-0 \ + wget \ + ruby && \ + + cd /tmp; wget wget https://dl.influxdata.com/influxdb/releases/influxdb_1.1.0_amd64.deb; dpkg -i influxdb_1.1.0_amd64.deb && \ + cd /tmp; wget https://grafanarel.s3.amazonaws.com/builds/grafana_3.1.1-1470047149_amd64.deb; dpkg -i grafana_3.1.1-1470047149_amd64.deb && apt-get -f install && \ + + rm -rf \ + /tmp/* \ + /var/lib/apt/lists/* \ + /var/tmp/* && \ + find /root -name . -o -prune -exec rm -rf -- {} + && \ + find /var/lib/gems -name "cache" -type d -exec rm -rf -- {} + && \ + mkdir -p \ + /root + +# import grafana dashboard +# ADD ./grafana/dash.json /var/lib/grafana/dashboards/dash.json + +# ports and volumes +#WORKDIR /opt/docker-readme-sync +EXPOSE 3000 8083 8086 +VOLUME /config + diff --git a/app/.ruby-gemset b/app/.ruby-gemset new file mode 100644 index 0000000..0e27efc --- /dev/null +++ b/app/.ruby-gemset @@ -0,0 +1 @@ +dockerhub-stats diff --git a/app/.ruby-version b/app/.ruby-version new file mode 100644 index 0000000..2bf1c1c --- /dev/null +++ b/app/.ruby-version @@ -0,0 +1 @@ +2.3.1 diff --git a/app/Gemfile b/app/Gemfile new file mode 100644 index 0000000..4280a60 --- /dev/null +++ b/app/Gemfile @@ -0,0 +1,9 @@ +source 'https://rubygems.org' + +ruby File.read(File.expand_path('../.ruby-version', __FILE__)).strip.sub(/ruby\-/, '') + +gem 'rake', '~> 11.3' +gem 'httparty', '~> 0.14.0' +gem 'influxdb', '~> 0.3.12' + +gem 'awesome_print' diff --git a/app/Gemfile.lock b/app/Gemfile.lock new file mode 100644 index 0000000..4103619 --- /dev/null +++ b/app/Gemfile.lock @@ -0,0 +1,28 @@ +GEM + remote: https://rubygems.org/ + specs: + awesome_print (1.7.0) + cause (0.1) + httparty (0.14.0) + multi_xml (>= 0.5.2) + influxdb (0.3.12) + cause + json + json (2.0.2) + multi_xml (0.5.5) + rake (11.3.0) + +PLATFORMS + ruby + +DEPENDENCIES + awesome_print + httparty (~> 0.14.0) + influxdb (~> 0.3.12) + rake (~> 11.3) + +RUBY VERSION + ruby 2.3.1p112 + +BUNDLED WITH + 1.13.6 diff --git a/app/config/settings.yml.example b/app/config/settings.yml.example new file mode 100644 index 0000000..01509c4 --- /dev/null +++ b/app/config/settings.yml.example @@ -0,0 +1,3 @@ +email: yourusernamehere +password: yourpasswordhere +org: yourorghere \ No newline at end of file diff --git a/app/stats.rb b/app/stats.rb new file mode 100644 index 0000000..a96807e --- /dev/null +++ b/app/stats.rb @@ -0,0 +1,40 @@ +require "httparty" +require "awesome_print" +require "influxdb" +require "yaml" + +config_file = File.dirname(__FILE__) + "/config/settings.yml" +config = YAML.load_file(config_file) + +email = config["email"] +password = config["password"] +org = config["org"] + +data = { username: email, + password: password } + +res = HTTParty.post("https://hub.docker.com/v2/users/login/", body: data).body +token = JSON.parse(res)["token"] + +headers = { token: token} +res = HTTParty.get( + "https://hub.docker.com/v2/repositories/#{org}/", + query: { page_size: 100 }, + headers: headers) + +data = JSON.parse(res.body) + +pull_counts = {} +data["results"].each do |repo| + pull_counts[repo["name"]] = repo["pull_count"] +end + +influxdb = InfluxDB::Client.new database: "dockerhub_stats" +pull_counts.each do |repo, count| + data = { + values: { pull_count: count }, + tags: { repo: repo } + } + + influxdb.write_point("dockerhub_stats", data) +end diff --git a/root/etc/cont-init.d/50-grafana.sh b/root/etc/cont-init.d/50-grafana.sh new file mode 100644 index 0000000..ae51130 --- /dev/null +++ b/root/etc/cont-init.d/50-grafana.sh @@ -0,0 +1,37 @@ +#!/usr/bin/with-contenv bash +set -e + +STAMP="/config/.grafana-setup-complete" + +if [ -f ${STAMP} ]; then + echo "grafana already configured, nothing to do." + exit 0 +fi + +mkdir -p /config/etc/grafana /config/var/lib/grafana /config/var/log/grafana + +cp /etc/grafana/grafana.ini /config/etc/grafana + +/usr/sbin/grafana-server --homepath=/usr/share/grafana --config=/config/etc/grafana/grafana.ini cfg:default.paths.data=/config/var/lib/grafana cfg:default.paths.logs=/config/var/log/grafana 2>&1 & + +until nc -z -v -w5 localhost 3000 < /dev/null; do sleep 1; done + +# create influxdb datasource +curl 'http://admin:admin@localhost:3000/api/datasources' \ + -X POST -H "Content-Type: application/json" \ + --data-binary <&1 & + +# wait for influxdb to respond to requests +until /usr/bin/influx -execute 'show databases'; do sleep 1; done +/usr/bin/influx -execute 'create database dockerhub_stats' + +kill `ps -ef | grep influxd | grep pid | awk {'print $2'}` + +touch ${STAMP} diff --git a/root/etc/cont-init.d/52-dockerhub_stats.sh b/root/etc/cont-init.d/52-dockerhub_stats.sh new file mode 100644 index 0000000..f74d7ee --- /dev/null +++ b/root/etc/cont-init.d/52-dockerhub_stats.sh @@ -0,0 +1,9 @@ +#!/usr/bin/with-contenv bash + +if [ ! -f /config/app/settings.yml ]; then + cp /opt/dockerhub-stats/config/settings.yml.example /config/app/settings.yml +fi + +if [ ! -f /opt/dockerhub-stats/config/settings.yml ]; then + ln -s /config/app/settings.yml /opt/dockerhub-stats/config/settings.yml +fi diff --git a/root/etc/services.d/grafana/run b/root/etc/services.d/grafana/run new file mode 100644 index 0000000..b0ad523 --- /dev/null +++ b/root/etc/services.d/grafana/run @@ -0,0 +1,3 @@ +#!/usr/bin/with-contenv bash + +/usr/sbin/grafana-server --homepath=/usr/share/grafana --config=/config/etc/grafana/grafana.ini cfg:default.paths.data=/config/var/lib/grafana cfg:default.paths.logs=/config/var/log/grafana diff --git a/root/etc/services.d/influxdb/run b/root/etc/services.d/influxdb/run new file mode 100644 index 0000000..451e603 --- /dev/null +++ b/root/etc/services.d/influxdb/run @@ -0,0 +1,3 @@ +#!/usr/bin/with-contenv bash + +/usr/bin/influxd -pidfile /var/run/influxdb/influxd.pid -config /config/etc/influxdb/influxdb.conf \ No newline at end of file From c7f2978a9597444694433f3cfe7dac9349b1db2c Mon Sep 17 00:00:00 2001 From: Paul Hendryx Date: Sun, 20 Nov 2016 15:29:28 -0500 Subject: [PATCH 2/2] added .gitignore --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ec7e05d --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +app/config/settings.yml +app/tmp +tmp +local-*.sh \ No newline at end of file