Skip to content

Commit f868382

Browse files
committed
cleanup proccessed files in VR
1 parent dea1373 commit f868382

5 files changed

Lines changed: 137 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.cloud.agent.api;
2+
3+
import com.cloud.exception.CloudException;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import java.util.stream.Collectors;
8+
9+
public class CleanProcessCommandCacheAnswer extends Answer {
10+
int numberOfFiles = 0;
11+
List<String> fileNames;
12+
13+
public CleanProcessCommandCacheAnswer(CleanProcessedCacheCommand cmd, String details) {
14+
super(cmd, new CloudException(details));
15+
}
16+
17+
public CleanProcessCommandCacheAnswer(CleanProcessedCacheCommand cmd, String details, boolean b) {
18+
super(cmd, true, details);
19+
String[] lines = details.split("\n");
20+
for (String line: lines) {
21+
parse(line);
22+
}
23+
}
24+
25+
void parse(String line) {
26+
if (line.startsWith("numberOfFiles:")) {
27+
String[] s = line.split(" ");
28+
numberOfFiles = Integer.parseInt(s[1]);
29+
} else if (line.startsWith("files:")) {
30+
String[] s = line.split(" ") ;
31+
fileNames = Arrays.stream(s, 1, s.length).collect(Collectors.toList());
32+
}
33+
}
34+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
20+
package com.cloud.agent.api;
21+
22+
import com.cloud.agent.api.routing.NetworkElementCommand;
23+
24+
public class CleanProcessedCacheCommand extends NetworkElementCommand {
25+
int days = 60;
26+
protected CleanProcessedCacheCommand () {
27+
}
28+
29+
public CleanProcessedCacheCommand (int days) {
30+
this.days = days;
31+
}
32+
33+
@Override
34+
public boolean executeInSequence() {
35+
return false;
36+
}
37+
38+
public int getDays() {
39+
return this.days;
40+
}
41+
}

core/src/main/java/com/cloud/agent/resource/virtualnetwork/VRScripts.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class VRScripts {
4242
public static final String IP_ALIAS_CONFIG = "ip_aliases.json";
4343
public static final String LOAD_BALANCER_CONFIG = "load_balancer.json";
4444

45+
public static final String CLEANUP_PROCESSED = "cleanup_processed.sh";
4546
public static final String SYSTEM_VM_PATCHED = "patched.sh";
4647
public final static String CONFIG_CACHE_LOCATION = "/var/cache/cloud/";
4748
public final static Duration VR_SCRIPT_EXEC_TIMEOUT = Duration.standardMinutes(10);

core/src/main/java/com/cloud/agent/resource/virtualnetwork/VirtualRoutingResource.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
import javax.naming.ConfigurationException;
3636

37+
import com.cloud.agent.api.CleanProcessCommandCacheAnswer;
38+
import com.cloud.agent.api.CleanProcessedCacheCommand;
3739
import com.cloud.agent.api.routing.UpdateNetworkCommand;
3840
import com.cloud.agent.api.to.IpAddressTO;
3941
import com.cloud.network.router.VirtualRouter;
@@ -445,6 +447,15 @@ private Answer execute(CheckRouterCommand cmd) {
445447
return new CheckRouterAnswer(cmd, result.getDetails(), true);
446448
}
447449

450+
private Answer execute(CleanProcessedCacheCommand cmd) {
451+
int days = cmd.getDays();
452+
final ExecutionResult result = _vrDeployer.executeInVR(cmd.getRouterAccessIp(), VRScripts.CLEANUP_PROCESSED, Integer.toString(days));
453+
if (!result.isSuccess()) {
454+
return new CleanProcessCommandCacheAnswer(cmd, result.getDetails());
455+
}
456+
return new CleanProcessCommandCacheAnswer(cmd, result.getDetails(), true);
457+
}
458+
448459
private Answer execute(DiagnosticsCommand cmd) {
449460
_eachTimeout = Duration.standardSeconds(NumbersUtil.parseInt("60", 60));
450461
final ExecutionResult result = _vrDeployer.executeInVR(cmd.getRouterAccessIp(), VRScripts.DIAGNOSTICS, cmd.getSrciptArguments(), _eachTimeout);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/bash
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
19+
days=60
20+
dir=/var/cache/cloud/processed
21+
22+
if ! [ $# -eq 0 ]; then
23+
re='^[0-9]+$'
24+
if ! [[ $1 =~ $re ]] ; then
25+
echo "error: argument must be a number and will be interpreted as a period in days" >&2; exit 1
26+
else
27+
days=$1
28+
fi
29+
if ! [ -z $2 ]; then
30+
dir=$2
31+
fi
32+
fi
33+
34+
files_to_delete=`find . -mtime +${days} 2>/dev/null`
35+
36+
if [ -z $files_to_delete ]
37+
then
38+
echo "numberOfFiles: 0"
39+
exit 0
40+
fi
41+
errors=`rm $files_to_delete >/dev/null`
42+
43+
if [ $? ]
44+
then
45+
echo -n "numberOfFiles: "
46+
echo $files_to_delete | wc -w
47+
echo "files: " $files_to_delete
48+
else
49+
echo "ERRORS: " $errors
50+
fi

0 commit comments

Comments
 (0)