remove systemOperate

This commit is contained in:
jysperm
2014-07-13 22:23:52 +08:00
parent 72e7ea8a77
commit 5b6a599fd3
7 changed files with 61 additions and 83 deletions

View File

@@ -14,9 +14,3 @@ exports.loadPlugins = (app) ->
if plugin.action
app.use ('/plugin/' + name), plugin.action
exports.systemOperate = (operator, callback) ->
if config.debug.mock_test
callback()
else
operator callback

View File

@@ -10,7 +10,7 @@ REDIS_OVERVIEW = 'rp:linux:overview'
ITEM_IN_RESOURCES_LIST = 3600 * 1000 / config.plugins.linux.monitor_cycle
last_plist = []
password_cache = {}
passwd_cache = {}
exports.run = ->
setInterval exports.monitoring, config.plugins.linux.monitor_cycle
@@ -19,12 +19,12 @@ exports.loadpassword = (callback) ->
fs.readFile '/etc/password', (err, content) ->
content = content.toString().split '\n'
password_cache = {}
passwd_cache = {}
for line in content
if line
[username, password, uid] = line.split ':'
password_cache[uid] = username
passwd_cache[uid] = username
callback()
@@ -41,8 +41,8 @@ exports.monitoring = ->
result = rx.exec item
return {
user: ->
if password_cache[result[1]]
return password_cache[result[1]]
if passwd_cache[result[1]]
return passwd_cache[result[1]]
else
return result[1]
pid: result[2]

View File

@@ -21,13 +21,10 @@ exports.post '/update_password/', (req, res) ->
unless req.body.password or not /^[A-Za-z0-9\-_]+$/.test req.body.password
return res.json 400, error: 'invalid_password'
plugin.systemOperate (callback) ->
connection = mysql.createConnection config.plugins.mysql.connection
connection.connect()
connection.query "SET PASSWORD FOR '#{req.account.username}'@'localhost' = PASSWORD('#{req.body.password}');", (err, rows, fields) ->
throw err if err
connection.end()
callback()
, ->
res.json {}
res.json {}

View File

@@ -10,41 +10,37 @@ mAccount = require '../../core/model/account'
module.exports =
enable: (account, callback) ->
plugin.systemOperate (callback) ->
connection = mysql.createConnection config.plugins.mysql.connection
connection.connect()
connection = mysql.createConnection config.plugins.mysql.connection
connection.connect()
connection.query "CREATE USER '#{account.username}'@'localhost' IDENTIFIED BY '#{mAccount.randomSalt()}';", (err, rows) ->
throw err if err
connection.end()
callback()
, callback
connection.query "CREATE USER '#{account.username}'@'localhost' IDENTIFIED BY '#{mAccount.randomSalt()}';", (err, rows) ->
throw err if err
connection.end()
callback()
delete: (account, callback) ->
plugin.systemOperate (callback) ->
connection = mysql.createConnection config.plugins.mysql.connection
connection.connect()
connection = mysql.createConnection config.plugins.mysql.connection
connection.connect()
connection.query "DROP USER '#{account.username}'@'localhost';", (err, rows) ->
connection.query "DROP USER '#{account.username}'@'localhost';", (err, rows) ->
throw err if err
connection.query 'show databases;', (err, rows) ->
throw err if err
connection.query 'show databases;', (err, rows) ->
throw err if err
databases_to_delete = _.filter _.pluck(rows, 'Database'), (item) ->
if item[..account.username.length] == "#{account.username}_"
return true
else
return false
databases_to_delete = _.filter _.pluck(rows, 'Database'), (item) ->
if item[..account.username.length] == "#{account.username}_"
return true
else
return false
async.each databases_to_delete, (name, callback) ->
connection.query "DROP DATABASE `#{name}`;", (err, rows) ->
throw err if err
callback()
, ->
connection.end()
async.each databases_to_delete, (name, callback) ->
connection.query "DROP DATABASE `#{name}`;", (err, rows) ->
throw err if err
callback()
, callback
, ->
connection.end()
callback()
widget: (account, callback) ->
jade.renderFile path.join(__dirname, 'view/widget.jade'), {}, (err, html) ->

View File

@@ -23,28 +23,26 @@ module.exports =
callback()
switch: (account, is_enable, callback) ->
plugin.systemOperate (callback) ->
callbackback = ->
child_process.exec 'sudo service php5-fpm restart', (err) ->
callbackback = ->
child_process.exec 'sudo service php5-fpm restart', (err) ->
throw err if err
callback()
if is_enable
tmp.file
mode: 0o750
, (err, filepath, fd) ->
config_content = _.template (fs.readFileSync path.join(__dirname, 'template/fpm-pool.conf')).toString(),
account: account
fs.writeSync fd, config_content, 0, 'utf8'
fs.closeSync fd
child_process.exec "sudo cp #{filepath} /etc/php5/fpm/pool.d/#{account.username}.conf", (err) ->
throw err if err
callback()
if is_enable
tmp.file
mode: 0o750
, (err, filepath, fd) ->
config_content = _.template (fs.readFileSync path.join(__dirname, 'template/fpm-pool.conf')).toString(),
account: account
fs.writeSync fd, config_content, 0, 'utf8'
fs.closeSync fd
child_process.exec "sudo cp #{filepath} /etc/php5/fpm/pool.d/#{account.username}.conf", (err) ->
throw err if err
callbackback()
else
child_process.exec "sudo rm /etc/php5/fpm/pool.d/#{account.username}.conf", ->
callbackback()
, callback
else
child_process.exec "sudo rm /etc/php5/fpm/pool.d/#{account.username}.conf", ->
callbackback()
widget: (account, callback) ->
jade.renderFile path.join(__dirname, 'view/widget.jade'), {account: account}, (err, html) ->

View File

@@ -19,9 +19,6 @@ exports.post '/update_password/', (req, res) ->
unless req.body.password or not /^[A-Za-z0-9\-_]+$/.test req.body.password
return res.error 'invalid_password'
plugin.systemOperate (callback) ->
child_process.exec "echo '#{req.account.username}:#{req.body.password}' | sudo chpassword", (err, stdout, stderr) ->
throw err if err
callback()
, ->
res.json {}
res.json {}

View File

@@ -6,26 +6,22 @@ plugin = require '../../core/plugin'
module.exports =
enable: (account, callback) ->
plugin.systemOperate (callback) ->
child_process.exec "sudo useradd -m -s /bin/bash #{account.username}", (err, stdout, stderr) ->
throw err if err
callback()
, callback
child_process.exec "sudo useradd -m -s /bin/bash #{account.username}", (err, stdout, stderr) ->
throw err if err
callback()
delete: (account, callback) ->
plugin.systemOperate (callback) ->
async.series [
(callback) ->
child_process.exec "sudo pkill -u #{account.username}", ->
callback()
async.series [
(callback) ->
child_process.exec "sudo pkill -u #{account.username}", ->
callback()
(callback) ->
child_process.exec "sudo userdel -rf #{account.username}", ->
callback()
], (err) ->
throw err if err
callback()
, callback
(callback) ->
child_process.exec "sudo userdel -rf #{account.username}", ->
callback()
], (err) ->
throw err if err
callback()
widget: (account, callback) ->
jade.renderFile path.join(__dirname, 'view/widget.jade'), {}, (err, html) ->