Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 59 lines (45 sloc) 1.643 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#!/usr/bin/env python

import ConfigParser
import os
import re
import shlex
import subprocess
import yaml

configfile = '/home/ubuntu/.s3cfg'

def exe(command, wait=True):
    """Execute a subprocess command"""

    # Open a subprocess to run your command
    process = subprocess.Popen(shlex.split(str(command)), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    if wait:
        read = process.communicate()
        return read
    else:
        return process

def s3_store():
    print "Draining the node..."
    exe('nodetool -h localhost drain')


    # Create the bucket
    exe('s3cmd mb s3://%s-%s' % (bucket_name, access_key))

    print "Uploading data..."
    response = exe('sudo s3cmd sync --delete-removed %s s3://%s-%s/%s/%s/' % (
        os.path.join(root_data_dir, 'data/'),
        bucket_name, access_key,
        cluster_name, initial_token
    ))
    if response[1]:
        print response[1]

    print "Stopping the node..."
    exe('sudo service dse stop')
    exe('sudo service cassandra stop')


# Read access_key
config = ConfigParser.RawConfigParser()
config.read(configfile)
access_key = config.get('default', 'access_key')
bucket_name = config.get('datastax', 'bucket_name') if config.has_option('default', 'bucket_name') else 'datastax_s3_storage'
root_data_dir = config.get('datastax', 'root_data_dir') if config.has_option('default', 'root_data_dir') else '/raid0/cassandra'

# Read cluster_name and initial_token
with open('/etc/dse/cassandra/cassandra.yaml') as f:
    dataMap = yaml.load(f)
cluster_name = re.sub(r'\W+', '_', dataMap['cluster_name'])
initial_token = dataMap['initial_token']

s3_store()
Something went wrong with that request. Please try again.