rakefile 6.1 KB
Newer Older
1 2 3
require 'rake/clean'
require 'tempfile'

4
# Build Constants
5 6
REPO_ROOT = File.dirname(__FILE__)
BUILD_DIR = File.join(REPO_ROOT, "build")
7
REPORT_DIR = File.join(REPO_ROOT, "reports")
8
LMS_REPORT_DIR = File.join(REPORT_DIR, "lms")
9 10

# Packaging constants
11
DEPLOY_DIR = "/opt/wwc"
12
PACKAGE_NAME = "mitx"
13
LINK_PATH = "/opt/wwc/mitx"
14
PKG_VERSION = "0.1"
15
COMMIT = (ENV["GIT_COMMIT"] || `git rev-parse HEAD`).chomp()[0, 10]
16
BRANCH = (ENV["GIT_BRANCH"] || `git symbolic-ref -q HEAD`).chomp().gsub('refs/heads/', '').gsub('origin/', '')
17 18 19
BUILD_NUMBER = (ENV["BUILD_NUMBER"] || "dev").chomp()

if BRANCH == "master"
20
    DEPLOY_NAME = "#{PACKAGE_NAME}-#{BUILD_NUMBER}-#{COMMIT}"
21
else
22
    DEPLOY_NAME = "#{PACKAGE_NAME}-#{BRANCH}-#{BUILD_NUMBER}-#{COMMIT}"
23 24 25
end
PACKAGE_REPO = "packages@gp.mitx.mit.edu:/opt/pkgrepo.incoming"

26 27
NORMALIZED_DEPLOY_NAME = DEPLOY_NAME.downcase().gsub(/[_\/]/, '-')
INSTALL_DIR_PATH = File.join(DEPLOY_DIR, NORMALIZED_DEPLOY_NAME)
28 29

# Set up the clean and clobber tasks
30
CLOBBER.include(BUILD_DIR, REPORT_DIR, 'cover*', '.coverage')
31
CLEAN.include("#{BUILD_DIR}/*.deb", "#{BUILD_DIR}/util")
32

33 34 35 36
def select_executable(*cmds)
    cmds.find_all{ |cmd| system("which #{cmd} > /dev/null 2>&1") }[0] || fail("No executables found from #{cmds.join(', ')}")
end

37 38 39 40
def django_admin(system, env, command, *args)
    django_admin = ENV['DJANGO_ADMIN_PATH'] || select_executable('django-admin.py', 'django-admin')
    return "#{django_admin} #{command} --settings=#{system}.envs.#{env} --pythonpath=. #{args.join(' ')}"
end
41

42
task :default => [:test, :pep8, :pylint]
43 44

directory REPORT_DIR
45
directory LMS_REPORT_DIR
46

47 48 49
desc "Run pep8 on all libraries"
task :pep8 => REPORT_DIR do
    sh("pep8 --ignore=E501 lms/djangoapps common/lib/* | tee #{REPORT_DIR}/pep8.report")
50 51
end

52 53 54 55
desc "Run pylint on all libraries"
task :pylint => REPORT_DIR do
    Dir["lms/djangoapps/*", "common/lib/*"].each do |app|
        ENV['PYTHONPATH'] = File.dirname(app)
56
        app = File.basename(app)
57
        sh("pylint --rcfile=.pylintrc -f parseable #{app} | tee #{REPORT_DIR}/#{app}.pylint.report")
Calen Pennington committed
58 59 60
    end
end

61 62 63 64 65
default_options = {
    :lms => '8000',
    :cms => '8001',
}

66
[:lms, :cms].each do |system|
67 68 69 70 71 72 73
    task_name = "test_#{system}"
    report_dir = File.join(REPORT_DIR, task_name)
    directory report_dir

    desc "Run all django tests on our djangoapps for the #{system}"
    task task_name => report_dir do
        ENV['NOSE_XUNIT_FILE'] = File.join(report_dir, "nosetests.xml")
74
        ENV['NOSE_COVER_HTML_DIR'] = File.join(report_dir, "cover")
75
        sh(django_admin(system, :test, 'test', *Dir["#{system}/djangoapps/*"].each))
76 77
    end
    task :test => task_name
78 79 80 81 82 83

    desc <<-desc
        Start the #{system} locally with the specified environment (defaults to dev).
        Other useful environments are devplus (for dev testing with a real local database)
        desc
    task system, [:env, :options] => [] do |t, args|
84
        args.with_defaults(:env => 'dev', :options => default_options[system])
85 86
        sh(django_admin(system, args.env, 'runserver', args.options))
    end
87 88 89 90 91
end

Dir["common/lib/*"].each do |lib|
    task_name = "test_#{lib}"

92
    report_dir = File.join(REPORT_DIR, task_name.gsub('/', '_'))
93 94 95
    directory report_dir

    desc "Run tests for common lib #{lib}"
96
    task task_name => report_dir do
97
        ENV['NOSE_XUNIT_FILE'] = File.join(report_dir, "nosetests.xml")
98
        sh("nosetests #{lib} --cover-erase --with-xunit --with-xcoverage --cover-html --cover-inclusive --cover-package #{File.basename(lib)} --cover-html-dir #{File.join(report_dir, "cover")}")
99 100
    end
    task :test => task_name
101
end
102

103 104
task :runserver => :lms

105 106 107 108 109 110
desc "Run django-admin <action> against the specified system and environment"
task "django-admin", [:action, :system, :env, :options] do |t, args|
    args.with_defaults(:env => 'dev', :system => 'lms', :options => '')
    sh(django_admin(args.system, args.env, args.action, args.options))
end

111
task :package do
112 113 114
    FileUtils.mkdir_p(BUILD_DIR)
    
    Dir.chdir(BUILD_DIR) do
115 116
        afterremove = Tempfile.new('afterremove')
        afterremove.write <<-AFTERREMOVE.gsub(/^\s*/, '')
John Jarvis committed
117
        #! /bin/bash
118 119 120 121
        set -e
        set -x
        
        # to be a little safer this rm is executed
John Jarvis committed
122
        # as the makeitso user
123

John Jarvis committed
124 125
        if [[ -d "#{INSTALL_DIR_PATH}" ]]; then
            sudo -u makeitso rm -rf "#{INSTALL_DIR_PATH}"
126 127 128 129 130
        fi

        AFTERREMOVE
        afterremove.close()
        FileUtils.chmod(0755, afterremove.path)
131 132 133 134 135 136

        postinstall = Tempfile.new('postinstall')
        postinstall.write <<-POSTINSTALL.gsub(/^\s*/, '')
        #! /bin/sh
        set -e
        set -x
137

138
        chown -R makeitso:makeitso #{INSTALL_DIR_PATH}
139

140
        service gunicorn stop || echo "Unable to stop gunicorn. Continuing"
141
        rm -f #{LINK_PATH}
142
        ln -s #{INSTALL_DIR_PATH} #{LINK_PATH}
143
        chown makeitso:makeitso #{LINK_PATH}
144

145
        # Delete mako temp files
146
        rm -rf /tmp/tmp*mako
147

148 149 150 151 152
        service gunicorn start
        POSTINSTALL
        postinstall.close()
        FileUtils.chmod(0755, postinstall.path)

153
        args = ["fakeroot", "fpm", "-s", "dir", "-t", "deb",
154
            "--verbose",
155
            "--after-install=#{postinstall.path}",
156
            "--after-remove=#{afterremove.path}",
157
            "--prefix=#{INSTALL_DIR_PATH}",
158 159 160
            "--exclude=**/build/**",
            "--exclude=**/rakefile",
            "--exclude=**/.git/**",
161
            "--exclude=**/*.pyc",
162
            "--exclude=**/reports/**",
163
            "-C", "#{REPO_ROOT}",
164
            "--provides=#{PACKAGE_NAME}",
165
            "--name=#{NORMALIZED_DEPLOY_NAME}",
166
            "--version=#{PKG_VERSION}",
167
            "-a", "all",
168
            "."]
169 170 171
        system(*args) || raise("fpm failed to build the .deb")
    end
end
172 173

task :publish => :package do
174
    sh("scp #{BUILD_DIR}/#{NORMALIZED_DEPLOY_NAME}_#{PKG_VERSION}*.deb #{PACKAGE_REPO}")
175
end
176 177 178 179 180 181 182 183 184 185 186 187

namespace :cms do
  desc "Import course data within the given DATA_DIR variable"
  task :import do
    if ENV['DATA_DIR']
      sh(django_admin(:cms, :dev, :import, ENV['DATA_DIR']))
    else
      raise "Please specify a DATA_DIR variable that point to your data directory.\n" +
        "Example: \`rake cms:import DATA_DIR=../data\`"
    end
  end
end