Skip to content

Instantly share code, notes, and snippets.

@aaronjudd
Last active August 29, 2015 14:04
Show Gist options
  • Save aaronjudd/ab60ca7fbbcdc0de36f6 to your computer and use it in GitHub Desktop.
Save aaronjudd/ab60ca7fbbcdc0de36f6 to your computer and use it in GitHub Desktop.
Example use of Rocker Docker from remote application
Meteor.startup ->
if Meteor.settings.rockerSettings.mongoUser
@mongodbUrl = "mongodb://" + Meteor.settings.rockerSettings.mongoUser + ":" + Meteor.settings.rockerSettings.mongoPassword + "@" + Meteor.settings.rockerSettings.launchMongoDB
else
@mongodbUrl = "mongodb://" + Meteor.settings.rockerSettings.launchMongoDB
@replicaSet = "?replicaSet=" + Meteor.settings.rockerSettings.replicaSet
@authSource = "&authSource=admin"
Meteor.methods
createDBUser: (sitename, password, email) ->
url = mongodbUrl + sitename + replicaSet + authSource
console.log url
db = Meteor._wrapAsync(MongoDB.connect.bind(MongoDB))(url)
#create new database and users collection in one step
try
result = Meteor._wrapAsync(db.createCollection.bind(db))("users")
catch error
console.error "Error creating new database during createCollection:", parseError(error)
return false
if not result
console.error "Error creating new database during createCollection: Unknown error"
return false
console.log sitename + " database/collection created"
# these should work for mongo 2.4 & 2.6+
options =
user: sitename
pwd: password
customData:
email: email
site: sitename
roles: [
{
role: "readWrite"
db: sitename
},
"readWrite"
]
# add user admin user for database
try
result = Meteor._wrapAsync(db.addUser.bind(db))(sitename,password,options)
catch error
console.error "Error calling createUser:", parseError(error)
db.close()
return false
if result[0].user
console.log "user created"
db.close()
return true
else
console.error "Error calling createUser:", result
db.close()
return false
createDriveAccount: (sitename,password,email) ->
options =
password: password
email: email
profile:
sitename: sitename
try
Accounts.createUser options
catch
# User already exists
console.log "drive user already exists"
return {created: false, existing: Meteor.users.findOne('emails.address': email)?.profile?.sitename}
console.log "drive user created"
return {created: true}
createSiteInstance: (sitename, password, email) ->
#private ip address of proxy
conn = DDP.connect(Meteor.settings.rockerSettings.ddpUrl)
MONGO_URL = "mongodb://" + sitename + ":" + password + "@" + Meteor.settings.rockerSettings.launchMongoDB + sitename + replicaSet
hostname = sitename.toLowerCase() + ".reactioncommerce.com"
try
DDP.loginWithPassword conn, {username: 'admin'}, Meteor.settings.rockerSettings.ddpPassword
catch error
console.log "DDP.loginWithPassword could not log in or could not access the rocker-docker server" + parseError(error)
conn.disconnect()
return false
try
result = Meteor._wrapAsync(conn.call.bind(conn))("ai/launch",
appImage: "ongoworks/reaction"
hostname: hostname
env:
MONGO_URL: MONGO_URL
METEOR_EMAIL: email
ROOT_URL: "https://" + hostname
MAIL_URL: ""
MONGO_OPLOG_URL: mongodbUrl + "local?authSource=admin"
)
catch error
console.log "error in ai/launch: " + parseError(error)
conn.disconnect()
if typeof result is "string"
console.log "launched app instance with ID", result, "mapped to", hostname
return !!result
parseError = (error) ->
result = ""
result += error.message + "; " if error.message
result += error.reason + "; " if error.reason
result += error.details if error.details
unless result.length > 0
result = "No error details found"
return result
@aldeed
Copy link

aldeed commented Jul 24, 2014

Note: DDP.loginWithPassword is provided by the ddp-login pkg, not currently published to atmosphere. They made backwards incompatible login changes to Meteor in 0.8.2, and rocker-docker is built for 0.8.2+, so this means the app you connect from using this code above must also be 0.8.2+.

@aaronjudd
Copy link
Author

Also, this is using https://www.npmjs.org/package/mongodb to establish the remote mongo connections (which is only needed if you are attempting to create remote databases,etc)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment