Skip to content

Commit

Permalink
Merge pull request #17 from alberts-tid/feature/chef-puppetNodeDelete
Browse files Browse the repository at this point in the history
development for chef & puppet node delete
  • Loading branch information
jesuspg committed May 5, 2014
2 parents b24d316 + 9119d07 commit 20aa56b
Show file tree
Hide file tree
Showing 32 changed files with 1,595 additions and 219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
Expand All @@ -46,6 +47,8 @@
import com.telefonica.euro_iaas.sdc.util.SystemPropertiesProvider;

public class InstallatorPuppetImpl implements Installator {

private static Logger log = Logger.getLogger("InstallatorPuppetImpl");

private HttpClient client;
private SystemPropertiesProvider propertiesProvider;
Expand All @@ -57,6 +60,8 @@ public void callService(VM vm, String vdc, ProductRelease product, String action
+ action + "/" + vdc + "/" + vm.getHostname() + "/" + product.getProduct().getName() + "/"
+ product.getVersion());

postInstall.addHeader("Content-Type", "application/json");

System.out.println("puppetURL: "+propertiesProvider.getProperty(SystemPropertiesProvider.PUPPET_MASTER_URL)
+ action + "/" + vdc + "/" + vm.getHostname() + "/" + product.getProduct().getName() + "/"
+ product.getVersion());
Expand All @@ -70,13 +75,17 @@ public void callService(VM vm, String vdc, ProductRelease product, String action
EntityUtils.consume(entity);

if (statusCode != 200) {
throw new InstallatorException(format("[install] response code was: {0}", statusCode));
String msg=format("[puppet install] response code was: {0}", statusCode);
log.info(msg);
throw new InstallatorException(format(msg));
}

// generate files in puppet master
HttpPost postGenerate = new HttpPost(
propertiesProvider.getProperty(SystemPropertiesProvider.PUPPET_MASTER_URL) + "generate/"
+ vm.getHostname());

postGenerate.addHeader("Content-Type", "application/json");

response = client.execute(postGenerate);
statusCode = response.getStatusLine().getStatusCode();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U <br>
* This file is part of FI-WARE project.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License.
* </p>
* <p>
* You may obtain a copy of the License at:<br>
* <br>
* http://www.apache.org/licenses/LICENSE-2.0
* </p>
* <p>
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* </p>
* <p>
* See the License for the specific language governing permissions and limitations under the License.
* </p>
* <p>
* For those usages not covered by the Apache version 2.0 License please contact with opensource@tid.es
* </p>
*/

/**
*
*/
package com.telefonica.euro_iaas.sdc.manager;

import com.telefonica.euro_iaas.commons.dao.EntityNotFoundException;
import com.telefonica.euro_iaas.sdc.exception.ChefClientExecutionException;
import com.telefonica.euro_iaas.sdc.exception.NodeExecutionException;
import com.telefonica.euro_iaas.sdc.model.dto.ChefClient;

/**
* @author jesus.movilla
*/
public interface NodeManager {

/**
* Delete a ChefClient in ChefServer
*
* @param chefClientname
* the name of the chefclient to be deleted from chef server
* @throws ChefClientExecutionException
*/
void nodeDelete(String vdc, String nodeName) throws NodeExecutionException;

/**
* Load a ChefClient from ChefServer
*
* @param chefClientname
* the name of the chefclient to be deleted from chef server
* @throws ChefClientExecutionException
* @throws EntityNotFoundException
*/
ChefClient chefClientload(String chefClientname) throws ChefClientExecutionException, EntityNotFoundException;

/**
* Loads the chefclient whose hostname is hostname
*
* @param hostname
* @return ChefClient
*/
ChefClient chefClientfindByHostname(String hostname) throws ChefClientExecutionException, EntityNotFoundException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U <br>
* This file is part of FI-WARE project.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License.
* </p>
* <p>
* You may obtain a copy of the License at:<br>
* <br>
* http://www.apache.org/licenses/LICENSE-2.0
* </p>
* <p>
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* </p>
* <p>
* See the License for the specific language governing permissions and limitations under the License.
* </p>
* <p>
* For those usages not covered by the Apache version 2.0 License please contact with opensource@tid.es
* </p>
*/

/**
*
*/
package com.telefonica.euro_iaas.sdc.manager.async;

import com.telefonica.euro_iaas.sdc.model.Task;

/**
* @author alberts
*/
public interface NodeAsyncManager {

/**
* Delete a NodeClient from ChefServer / PuppetMaster
*
* @param nodeName
* the name of the node to be deleted from chef server / puppet master
* @param task
* the task which contains the information about the async execution
* @param callback
* if not empty, contains the url where the result of the execution will be sent
*/
void nodeDelete(String vdc, String chefClientname, Task task, String callback);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/**
* Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U <br>
* This file is part of FI-WARE project.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License.
* </p>
* <p>
* You may obtain a copy of the License at:<br>
* <br>
* http://www.apache.org/licenses/LICENSE-2.0
* </p>
* <p>
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* </p>
* <p>
* See the License for the specific language governing permissions and limitations under the License.
* </p>
* <p>
* For those usages not covered by the Apache version 2.0 License please contact with opensource@tid.es
* </p>
*/

/**
*
*/
package com.telefonica.euro_iaas.sdc.manager.async.impl;

import static com.telefonica.euro_iaas.sdc.util.SystemPropertiesProvider.CHEF_NODE_BASE_URL;

import java.text.MessageFormat;
import java.util.Date;
import java.util.logging.Logger;

import org.apache.commons.lang.StringUtils;

import com.telefonica.euro_iaas.sdc.exception.NodeExecutionException;
import com.telefonica.euro_iaas.sdc.manager.NodeManager;
import com.telefonica.euro_iaas.sdc.manager.async.NodeAsyncManager;
import com.telefonica.euro_iaas.sdc.manager.async.TaskManager;
import com.telefonica.euro_iaas.sdc.model.Task;
import com.telefonica.euro_iaas.sdc.model.Task.TaskStates;
import com.telefonica.euro_iaas.sdc.model.TaskError;
import com.telefonica.euro_iaas.sdc.model.TaskReference;
import com.telefonica.euro_iaas.sdc.util.SystemPropertiesProvider;
import com.telefonica.euro_iaas.sdc.util.TaskNotificator;

/**
* @author alberts
*/
public class NodeAsyncManagerImpl implements NodeAsyncManager {

private static Logger LOGGER = Logger.getLogger(NodeAsyncManagerImpl.class.getName());

private TaskManager taskManager;
private SystemPropertiesProvider propertiesProvider;
private TaskNotificator taskNotificator;
private NodeManager nodeManager;

public void nodeDelete(String vdc, String nodeName, Task task, String callback) {
try {
nodeManager.nodeDelete(vdc, nodeName);
updateSuccessTask(task, vdc, nodeName);
LOGGER.info("Node " + nodeName + " is being deleted");
} catch (NodeExecutionException e) {
updateErrorTask(vdc, nodeName, task, "The node " + nodeName
+ " can not be deleted due to an error executing in node.", e);
} catch (Throwable e) {
updateErrorTask(vdc, nodeName, task, "The node " + nodeName
+ " can not be deleted due to unexpected error.", e);
} finally {
notifyTask(callback, task);
}
}

// //////// PRIVATE METHODS ///////////

/*
* Update the task with necessary information when the task is success.
*/
private void updateSuccessTask(Task task, String vdc, String chefClientname) {
String piResource = MessageFormat.format(propertiesProvider.getProperty(CHEF_NODE_BASE_URL), vdc,
chefClientname); // the product
task.setResult(new TaskReference(piResource));
task.setEndTime(new Date());
task.setStatus(TaskStates.SUCCESS);
taskManager.updateTask(task);
}

/*
* Update the task with necessary information when the task is wrong and the product instance exists in the system.
*/
private void updateErrorTask(String vdc, String chefClientname, Task task, String message, Throwable t) {
String piResource = MessageFormat.format(propertiesProvider.getProperty(CHEF_NODE_BASE_URL), vdc,
chefClientname); // the product
task.setResult(new TaskReference(piResource));
updateErrorTask(task, message, t);
}

/*
* Update the task with necessary information when the task is wrong.
*/
private void updateErrorTask(Task task, String message, Throwable t) {
TaskError error = new TaskError(message);
error.setMajorErrorCode(t.getMessage());
error.setMinorErrorCode(t.getClass().getSimpleName());
task.setEndTime(new Date());
task.setStatus(TaskStates.ERROR);
task.setError(error);
taskManager.updateTask(task);
LOGGER.info("An error occurs while deleting a node . See task " + task.getHref()
+ "for more information");
}

private void notifyTask(String url, Task task) {
if (!StringUtils.isEmpty(url)) {
taskNotificator.notify(url, task);
}
}

// ////////// I.O.C ////////////

/**
* @param chefClientManager
* the chefClientManager to set
*/
public void setNodeManager(NodeManager nodeManager) {
this.nodeManager = nodeManager;
}

/**
* @param taskManager
* the taskManager to set
*/
public void setTaskManager(TaskManager taskManager) {
this.taskManager = taskManager;
}

/**
* @param propertiesProvider
* the propertiesProvider to set
*/
public void setPropertiesProvider(SystemPropertiesProvider propertiesProvider) {
this.propertiesProvider = propertiesProvider;
}

/**
* @param taskNotificator
* the taskNotificator to set
*/
public void setTaskNotificator(TaskNotificator taskNotificator) {
this.taskNotificator = taskNotificator;
}

}
Loading

0 comments on commit 20aa56b

Please sign in to comment.