Skip to content

Instantly share code, notes, and snippets.

@mrunalp
Created May 23, 2013 04:09
Show Gist options
  • Save mrunalp/5632683 to your computer and use it in GitHub Desktop.
Save mrunalp/5632683 to your computer and use it in GitHub Desktop.
Cartridge generator
#!/usr/bin/env ruby
# Usage ./cartgen.rb <cart_name>
require 'fileutils'
require 'logger'
$log = Logger.new(STDERR)
cart_name = ARGV[0]
cart_short_name = cart_name.upcase
$log.info "Generating bare cartridge for #{cart_name}"
# Create the minimum required directories
FileUtils.mkdir(cart_name)
%w[bin env metadata template].each do |dir|
FileUtils.mkdir_p("#{cart_name}/#{dir}")
end
def write_file(file_path, contents, options = {})
if options[:exec]
mod = 0755
else
mod = 0644
end
File.open(file_path, 'w', mod) do |f|
f.write(contents)
end
end
# Create setup/install file
setup_contents = <<-EOC
#!/bin/bash -e
source $OPENSHIFT_CARTRIDGE_SDK_BASH
EOC
install_contents = <<-EOC
#!/bin/bash -e
source $OPENSHIFT_CARTRIDGE_SDK_BASH
mkdir -p $OPENSHIFT_#{cart_short_name}_DIR/logs
client_result "#{cart_name} added to the application"
EOC
# Add a control file
control_contents = <<-EOC
#!/bin/bash -e
source $OPENSHIFT_CARTRIDGE_SDK_BASH
function start {
echo "Starting #{cart_name}"
}
function stop {
echo "Starting #{cart_name}"
}
function restart {
echo "Restarting #{cart_name}"
}
function status {
client_result '#{cart_name} is running/stopped'
}
function tidy() {
client_message "Emptying log dir: $OPENSHIFT_#{cart_short_name}_LOG_DIR"
shopt -s dotglob
rm -rf $OPENSHIFT_#{cart_short_name}_LOG_DIR/*
}
case "$1" in
start) start ;;
stop) stop ;;
restart) restart ;;
status) status ;;
tidy) tidy ;;
esac
EOC
# Add a sample env variable
env_contents = <<-EOC
<%= ENV['OPENSHIFT_#{cart_short_name}_DIR'] %>/logs/
EOC
# Generate the manifest.yml
manifest_contents = <<-EOC
Name: #{cart_name}
Cartridge-Short-Name: #{cart_short_name}
Display-Name: #{cart_name.capitalize} 0.1
Description: "Description of the cart"
Version: 0.1
License: GPLv2
Vendor: Red Hat
Cartridge-Version: 0.0.1
Cartridge-Vendor: redhat
Website: "http://www.#{cart_name}.net/"
Categories:
- embedded
Cart-Data:
- Key: connection_url
Type: cart_data
Description: "#{cart_name} connection URL"
Provides:
- #{cart_name}-0.1
- "#{cart_name}"
Publishes:
get-serve-info:
Type: "NET_TCP:db-config:url"
Subscribes:
set-db-connection-info:
Type: "NET_TCP:db:connection-info"
Required: false
set-mysql-connection-info:
Type: "NET_TCP:db:mysql"
Required : true
Scaling:
Min: 1
Max: 1
Configure-Order:
- mysql
- #{cart_name}
Endpoints:
- Private-IP-Name: IP
Private-Port-Name: PORT
Private-Port: 8080
Public-Port-Name: PROXY_PORT
Mappings:
- Frontend: ""
Backend: ""
Install-Build-Required: false
EOC
# Generate sample index.html template file
template_file_contents = <<-EOC
<!DOCTYPE html>
<html>
<body>
<h1>Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
EOC
spec_file_contents = <<-EOC
%global cartridgedir %{_libexecdir}/openshift/cartridges/v2/#{cart_name}
Summary: #{cart_name} support for OpenShift
Name: openshift-origin-cartridge-#{cart_name}
Version: 0.1
Release: 1%{?dist}
Group: Applications/Internet
License: ASL 2.0
URL: https://www.openshift.com
Source0: http://mirror.openshift.com/pub/openshift-origin/source/%{name}/%{name}-%{version}.tar.gz
Requires: rubygem(openshift-origin-node)
BuildArch: noarch
%description
Provides #{cart_name} cartridge support. (Cartridge Format V2)
%prep
%setup -q
%build
%install
rm -rf %{buildroot}
mkdir -p %{buildroot}%{cartridgedir}
cp -r * %{buildroot}%{cartridgedir}/
%clean
rm -rf %{buildroot}
%files
%defattr(-,root,root,-)
%dir %{cartridgedir}
%attr(0755,-,-) %{cartridgedir}/bin/
%attr(0755,-,-) %{cartridgedir}
%{cartridgedir}/metadata/manifest.yml
%doc %{cartridgedir}/README.md
changelog
EOC
write_file(File.join(cart_name, 'bin', 'setup'), setup_contents, exec: true)
write_file(File.join(cart_name, 'bin', 'install'), install_contents, exec: true)
write_file(File.join(cart_name, 'bin', 'control'), control_contents, exec: true)
write_file(File.join(cart_name, 'env', "OPENSHIFT_#{cart_short_name}_LOG_DIR.erb"), env_contents)
write_file(File.join(cart_name, 'metadata', 'manifest.yml'), manifest_contents)
write_file(File.join(cart_name, 'template', 'index.html'), template_file_contents)
write_file(File.join(cart_name, "openshift-origin-cartridge-#{cart_name}.spec"), spec_file_contents)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment